我正在尝试读取一个 txt 文件,我可以得到我想要的行,但是我不能一个一个地打印这一行中的每个单词;
例如:该行看起来像:
hello world 1 2 3
我需要一张一张打印出来,看起来像:
hello
world
1
2
3
我得到了分段错误核心转储错误
char temp[256];
while(fgets(temp, 256, fp) != NULL) {
...
int tempLength = strlen(temp);
char *tempCopy = (char*) calloc(tempLength + 1, sizeof(char));
strncpy(temCopy, temp, tempLength); // segmentation fault core dumped here;
// works fine with temp as "name country"
name = strtok_r(tempCopy, delimiter, &context);
country = strtok_r(Null, delimiter, &context);
printf("%s\n", name);
printf("%s\n", country);
}
谁能帮我修复代码?
谢谢!