最近我正在閱讀Deitel編寫的C How to Program,第7版的文件處理部分。對於寫入文件,它使用這個例子:C程序打印最後一行兩次(文件I/O)
// Fig. 11.2: fig11_02.c
// Creating a sequential file
#include <stdio.h>
int main(void)
{
unsigned int account; // account number
char name[ 30 ]; // account name
double balance; // account balance
FILE *cfPtr; // cfPtr = clients.dat file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("clients.dat", "w")) == NULL) {
puts("File could not be opened");
} // end if
else {
puts("Enter the account, name, and balance.");
puts("Enter EOF to end input.");
printf("%s", "? ");
scanf("%d%29s%lf", &account, name, &balance);
// write account, name and balance into file with fprintf
while (!feof(stdin)) {
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
printf("%s", "? ");
scanf("%d%29s%lf", &account, name, &balance);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
,你可以看到,它會掃描並打印數據先在別的塊,然後在while循環。因爲我認爲做兩遍沒有意義,它只是刪除了if-else部分並編譯它。以及它工作正常,但後來我意識到它重複輸出文件中的最後一行輸入。在Deitel版本中沒有。
我的版本有什麼問題?爲什麼它複製最後一行?我認爲這可能是一個循環條件的問題,但我不知道..
編輯:此代碼是由Dietel使用,我不相信這是錯誤的,因爲他正在使用if-else來修復引起的問題通過!feof。但我想知道如何解決它,而沒有,如果 - 其他。
在編輯的版本沒有的if-else,只帶環的輸入和輸出是:
輸入:
1 test 25.6
2 some 95
輸出:
1 test 25.6
2 some 95
2 some 95
可能重複(http://stackoverflow.com/questions/5431941/while-feof-文件是總是錯誤的) – 2015-03-19 11:26:53
我讀過,但我認爲我的情況是不同的。 – vvvsg 2015-03-19 11:29:12
在完成'main'函數之前,您應該輸入'return 0;'或某個數字。 – Gophyr 2015-03-19 11:38:58