[英]Why doesn't this function return the last full line in a serial file?
[英]fgetc doesn't return last line of my file
這是我第一次編寫C語言,正在嘗試打開文件並讀取其內容。 我正在使用fgetc來獲取我的.txt文件的行,但是它僅返回內容,而沒有我的.txt文件的最后一行。 如何確保fgetc返回最后一行?
FILE *fp = fopen("nums.txt", "r");
if(fp)
{
int c = fgetc(fp);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fp);
}
}
預期結果是:
12
13
14
但它會返回到控制台,如下所示:
12
13
您可以使用setbuf(stdout, NULL);
在循環之前禁用緩沖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.