[英]C - file read don't print last line from file where i use multiple file to write
I wrote ac program which read from a input file and then print each line to the standard output but it don't print the last line of the file! 我编写了一个ac程序,该程序从输入文件中读取内容,然后将每一行打印到标准输出中,但不会打印文件的最后一行!
int main() {
FILE *rf = fopen("input_text.txt", "r");
char c;
if (rf) {
while ((c = getc(rf)) != EOF) {
putchar(c);
}
fclose(rf);
}
return 0;
}
How can i fix this issue? 我该如何解决这个问题? Thanks in Advance! 提前致谢!
You likely need to flush the output stream because it is being buffered. 您可能需要刷新输出流,因为它正在被缓冲。 Add a call to fflush(stdout);
添加对fflush(stdout);
的调用fflush(stdout);
just before fclose: 在fclose之前:
int main() {
FILE *rf = fopen("input_text.txt", "r");
int c;
if (rf) {
while ((c = getc(rf)) != EOF) {
putchar(c);
}
fflush(stdout);
fclose(rf);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.