爲什麼下面的代碼不起作用?我的意思是,它在控制檯輸出上顯示各種奇怪的字符。爲什麼我需要刷新我的I/O流才能獲得正確的結果?
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
但是,下面的代碼運行良好。
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fflush (pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
爲什麼我需要刷新流以獲得正確的結果?
Thx很多!!!!!! – Jaebum 2010-02-09 16:47:05
但爲什麼第二個來源的輸出不是「測試」? 它只是沒有顯示任何內容。 – Jaebum 2010-02-09 17:04:15
@Lee:簡單的說,你從你寫作的地方開始閱讀,你需要回到開始工作。 – falstro 2010-02-09 17:22:23