大家好,我正在尝试逐字逐句读取一个大的txt文件,然后打印出每个单词然后继续循环直到EOF,但运行此代码后我没有输出。我检查一切,文件名是正确的,该文件也与我的c文件在同一个文件夹中。有人可以解释一下发生了什么吗?谢谢。这是txt文件和代码:
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *infile;
char temp_1[25];
setvbuf(stdout, NULL, _IONBF, 0);
infile = fopen("LittleRegiment.txt", "r");
if(infile != NULL) {
while(fscanf(infile, "%s", temp_1) != EOF) {
printf("%s ", temp_1);
}
} else {
printf("Couldn't open the file.");
}
return 0;
}
答案 0 :(得分:2)
尝试打印错误原因。
} else {
//printf("Couldn't open the file.");
perror("open file"); // prototype in <stdio.h>
}