【发布时间】:2025-12-19 07:50:12
【问题描述】:
我正在使用strtok() 从从文件中获取的数据中提取字符。
这是我的代码:
fgets(text, 12, myFile);
printf ("text is: %s \n", &text);
char *token;
token = strtok(text, " ");
printf("first token is: %s \n", &token);
printf ("text is: %s \n", &text);
token = strtok(NULL, " ");
printf("second token is: %s \n", &token);
输出如下:
text is: 4 3 //this is the expected and correct value of "text"
first token is: ڱ[?
text is: 4 // I was expecting this to be 3 after getting the first token...
second token is: "ڱ[?
Segmentation fault: 11
如您所见,strtok() 不仅没有得到正确的值,而且似乎以非常奇怪的顺序遍历了文本。关于为什么会这样的任何想法?非常感谢您!
【问题讨论】:
-
text是char指针还是char的数组?我希望是后者。