1
調試程序一段時間後,我意識到有一些問題,函數與fgets()在C:在「耗盡」後無法使用fgets()讀取文件?
示例代碼:
....
char arr[50];
File* file=fopen("mytext.txt","r");
for(int i=0;i<=100;++i){
....
fgets(arr,sizeof(arr),file); //I use a for loop here for example
.... //just to illustrate that I use the
...do some stuff... //fgets() function lots of times
}
fgets(arr,sizeof(arr),file); //And now I use it again but get nothing
//because I have used the fgets() to consume the file up
....
所以你看,我想在這裏說些什麼是:當你使用fgets()從文件中獲取數據太多時,它會到達文件的末尾。然後當你再次使用它時,它不會返回任何東西(只是一個空指針,但沒有錯誤!!)
我在調試一段時間後發現問題。我注意到,其他formattd輸入函數也有相同的問題,如scanf()。
所以我想知道是否有一種方法來刷新機器,並使新的東西,使我可以再次使用fgets()函數。 謝謝。
謝謝;我知道這個功能絕對沒有問題。我只需要這樣表達事情,因爲我不是以英語爲母語的人。不過謝謝。 – walkerlala