首页 文章
  • 0 votes
     answers
     views

    使用strtok拼接cstrings,仅适用于首次执行循环

    我试图使用strtok将一行读入cstring拼接成单个字符串 . 是的,我知道使用字符串对象可以更轻松地完成此操作,但我不允许使用它们 . 当这段代码执行时,它在第一行完美地工作,然后它继续只对循环的每次迭代在同一行上工作 . 该文件正被成功读入'line'字符数组中,正如cout << line << endl所证明的那样;然而,strtok和第二个while循环继续分...
  • 2 votes
     answers
     views

    这段小代码中的分段错误?

    char *commandstrings[MAXARGS]; commandstr = strtok(line,"|"); int i = 0; while(commandstr != NULL){ commandstrings[i] = commandstr; printf("%s \n",command...
  • 0 votes
     answers
     views

    为什么我的代码在我标记它时会返回(null)?

    给了我一个要读取的文件,并将内容存储在链表中 . 文件schedule.csv以逗号分隔,包含: CSE1325.001,1,0,1,0,1,10:00,11:00 CSE1325.002,0,1,0,1,0,12:30,14:00 CSE2312.001,0,1,0,1,0,14:00,15:30 CSE2315.001,1,0,1,0,1,09:00,10:00 CSE2315.002,0,1...
  • 0 votes
     answers
     views

    从不同的文件中读取并在字符串上使用strtok

    所以这是一个代码,从2个不同的文件中读取3个字符串 (orig // test1 // orig_copy) **(firstline // secondline)**and calls divide_string to use strtok and take tokens and store them in (token_orig // token_test // token_orig_co...
  • 0 votes
     answers
     views

    strtok如何正常工作?

    我有一个我想在c中读取的文件,文件的格式如下: <city> <pokemon1> <pokemon2>; <near_city1> <near_city2> 例如: paris pidgey pikachu; london berlin 我希望能够使用strtok将此行切割为令牌,但由于某种原因,它无法正常工作 . 我的代码:假设我使...
  • 11 votes
     answers
     views

    在C中的循环中使用strtok()?

    我试图在嵌套循环中使用strtok() . 但这并没有给我预期的结果 . 可能是因为他们使用相同的内存位置 . 我的代码形式如下: - char *token1 = strtok(Str1, "%"); while(token1 != NULL ) { char *token2 = strtok(Str2, "%"); while(token2 !=...
  • 5 votes
     answers
     views

    在程序中一起使用getline和strtok的问题

    在下面的程序中,我打算将文件中的每一行读成一个字符串,分解字符串并显示单个单词 . 我面临的问题是,程序现在只输出文件中的第一行 . 我不明白为什么会这样? #include<iostream> #include<string> #include<fstream> #include<cstdio> using namespace std; int ...
  • 2 votes
     answers
     views

    如何在for循环中使用strtok()?

    对于给定的代表, typedef struct { int age; char *firstName; char *lastName; }Record; 给予 file.txt , Age,LastName,FirstName 50,B,A 30,A,B 20,X,D 10,F,A 90,V,E 60,N,M 以下是 main() 中的代码, pFile=fopen(&quot...
  • 0 votes
     answers
     views

    C - 使用strtok的嵌套循环

    我正在尝试使用strtok将文本文件拆分为可以传递给拼写检查功能的字符串,文本文件包含'\ n','?!, . 等字符 . 等...我需要打印任何未通过拼写检查的单词以及它们所在的行号 . 跟踪线路是我正在努力的方向 . 到目前为止我已经尝试了这个,但它只返回文本文件第一行的结果: char str[409377]; fread(str, noOfChars, 1, file); fclose(f...
  • 0 votes
     answers
     views

    c,一起使用strtok和strcat

    我正在使用c创建自己的shell但我一直收到错误,我认为这涉及使用 strtok 和 strcat . 请注意 path 和 userInput 是全局字符串 . int myFunction() { char *possiblePaths = getenv(PATH); path = strtok(possiblePaths,":"); path ...
  • 1 votes
     answers
     views

    strtok只返回一个令牌

    我正在编写一个简单的shell,它接受一些标准命令,比如C中的cd和ls . 我正在尝试实现一个用户可以输入“;”的功能 . 在命令之间,以便可以在同一行上写入一堆命令并单独执行 . 因此,如果我输入“cd Desktop; ls”,shell应该cd到Desktop并打印目录中的内容 . 问题是它只执行第一个命令 . 这是我的主要方法: char input[1024]; while(1) {...
  • 1 votes
     answers
     views

    如何用C中的换行符解析字符串?

    我正在写一个shell,我正在使用getline()和键盘中的stdin来接受命令 . 我虽然难以对输入进行标记 . 我尝试在strtok()函数中使用\ n作为分隔符,但似乎没有工作 . 例如,我包含一个if语句来检查用户是否键入“exit”,在这种情况下它将终止程序 . 它没有终止 . 这是我正在使用的代码: void main() { int ShInUse = 1; char *UserC...
  • 0 votes
     answers
     views

    使用strtok()和strcmp()的分段错误错误

    我正在尝试制作一个简单的C程序,它逐行检查Linux密码文件,搜索以命令行参数提供的用户名开头的行 . 每行包含由冒号分隔的几个标记 . 第一个令牌是用户名,第二个是无关紧要,第三个是需要打印的用户ID(UID)号,第四个令牌是需要打印的组ID号(GID) . 使用一些打印测试并在线搜索解决方案,我认为我的令牌变量在将其分配给我的第一个strtok调用后仍为NULL(此时令牌的printf不打印任...
  • -1 votes
     answers
     views

    使用getline()进行用户输入后对字符串进行标记?

    我正在尝试使用getline()接收输入后从用户输入标记字符串 . 我的代码如下 . 但是,一旦我开始标记化,我就会遇到分段错误 . 我应该采用不同的方式进行令牌化,还是我在进行令牌化错误? int main(int argc, char **argv) { int bytes_read = 1; int nbytes = 10; char *command, *flag, *path...
  • 0 votes
     answers
     views

    使用do while循环到输入文件中的下一行

    基本上我想要做的是使用数组和strtok从我工作的输入文件中提取时间和IP地址 . 我的问题是输入文件是一个很长的列表,我需要为.txt文件中的每一行执行此操作,但每当我尝试将我的代码放入带有feof(input_poiter)的do while循环时,它将不再执行一次,继承人我所拥有的; #include <stdlib.h> #include <string.h> #i...
  • -1 votes
     answers
     views

    使用strcpy()时出现分段错误

    我们添加了print语句来检查分段错误发生的位置 . 它在strcpy(命令,令牌)失败;我们如何将该部分存入指令?还有一种方法可以检查令牌末尾的空字符吗? strtok()在使用时是否有空字符? int main(int argc, char **argv) { char *command, *flag, *pathname, *linkname; struct stat st = {0...
  • 0 votes
     answers
     views

    从字符串中标记字符串时出错

    我想从SIC汇编代码生成符号表 . 为此,我试图将每个字符串从汇编代码中分离出来 . 因此,在尝试第一步时,我尝试了这段代码 . Here, What I have tried is to read the assembly code form a file line by line. And then to separate the strings in the line to tokens. ...
  • 0 votes
     answers
     views

    在循环中一起使用getline和strtok

    我是C的新手,并试图实施whoami,作为对自己的锻炼 . 我有以下代码: #define _POSIX_SOURCE #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <string.h> // strtok int str_to_int(const ch...
  • 1 votes
     answers
     views

    对来自getline的输入进行标记

    我正在尝试使用getline()从键盘获取输入,将其存储在字符串中,对其进行标记,然后打印标记 . 当我运行它时,我在最后一次迭代(处理来自输入的最后一个令牌的迭代)上得到一个Segmentation Fault错误 . #define _POSIX_C_SOURCE 200809L #include <stdio.h> #include <stdlib.h> #inclu...
  • 1 votes
     answers
     views

    结构数组 - C值不好

    我在C中面临一个非常有趣的问题所以我有一个看起来像这样的结构: struct Question { int year; char* month; char* day; char* hour; char* minute; char* second; char* question; int answerCount; char* a...
  • 0 votes
     answers
     views

    当返回-1时,strtok消失了

    所以我正在编写代码将字符串放入数组中并且它工作正常,但是当我在文件中按##时,我希望它终止字符串的读取 . 我正在运行循环并逐行解析字符串 . 在我的字符串解析器中,我放了一个循环来检查## . 它位于我的解析器函数的最后,它会: for (i = 0; i < strlen(line)); i++) { if ((buffer[i] == '#') && (buff...
  • 1 votes
     answers
     views

    在C语言中使用strtok

    我在C中使用strtok时遇到问题 . 我使用fgets从命令行获取用户输入,我想用管道(“|”)作为分隔符对其进行标记,并将结果放在双指针变量中 . 这是我的代码: char** argv; char *token; token = strtok(userInput, "|"); while(token != NULL){ *(argv++) = token; to...
  • 0 votes
     answers
     views

    Strtok解析并存储到阵列中

    我正在尝试解析一些包含一些文件名作为参数的字符串 . 我的目标是 parse 命令及其参数和 store 它到2d char数组 . 示例输入: /* Where 'cmd' is a command, file* are the arguments */ cmd file1 file2 file3 这是我的代码: int8_t **file_names = NULL; /* Allocate...
  • -2 votes
     answers
     views

    使用strtok吐出数组并使用唯一的空终止字符串存储标记

    我有一个字符串数组cmd,容量为255 chars这样声明: char cmd[255]; 我使用fgets以这种方式获取用户输入: fgets(cmd, 256, stdin); 我有三个独立的数组来存储三个令牌(假设用户只输入一个最多2个空格的字符串)这样声明: char arg[20]; char arg2[20]; char arg3[20]; 我用这样的方式用strtok拆分cmd...
  • 0 votes
     answers
     views

    调用fgets从文件读取行时出现分段错误

    在调用fgets大约20次之后,我遇到了一个段错误 . 我正在打开一个文件(不返回null) . 它的格式为: num1: value1 num2: value2 num3: value3 然后从文件中读取行,将值存储到数组中,使用nums作为位置 . 以下是seg fault的代码: 编辑:声明myArray和line: char myArray[3000]; char * lin...
  • 1 votes
     answers
     views

    如何为C中的字符串数组动态分配内存?

    我在C中阅读了以前关于动态数组的问题但是我无法将答案与我的问题联系起来 . 我使用fgets从 stdin 获取命令,删除换行符然后想要在动态分配的字符串数组中存储由空格分隔的每个命令 . 然而,我在分配和重新分配内存的正确方法上遇到了很多麻烦 . 我正在编译 clang 并继续得到分段错误11.然后我使用 -fsanitize=address 并继续获取: == 2286 ==错误:Addre...

热门问题