作者neverchoose (不曾選擇)
看板C_and_CPP
標題[問題] 突然回來寫C的時候發現的鬼打牆問題
時間Thu Apr 9 16:53:03 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
按一個數字後按ENTER 如此反覆幾次後把程式關閉
餵入的資料(Input):
預期的正確結果(Expected Output):
如按的數字依序存在TXT檔裡
錯誤結果(Wrong Output):
TXT檔裡什麼都沒有
程式碼(Code):(請善用置底文網頁, 記得排版)
int main(void)
{
FILE *inputfile;
int day;
inputfile=fopen("alin.txt","w");
while(1)
{
scanf("%d", &day);
getchar();
fprintf(inputfile,"%d\n",day);
}
fclose(inputfile);
system("pause");
return 0;
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.219.133.73
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1428569585.A.620.html
推 bill42362: #include <stdio.h> 04/09 17:29
→ bill42362: while 讓他正常結束就會寫入了 @@" 04/09 17:30
推 WYchuang: 你在while 迴圈裡面加入 sync()試看看吧 04/09 17:34
推 LPH66: 都 FILE* 了就用 fflush 吧 04/09 19:37
→ LPH66: 不過還是照二樓說的比較對 04/09 21:38
推 trylovetom: While出不了 04/10 11:25
→ trylovetom: Scanf放到while的判斷式裏,scanf跟getchar不要一起 04/10 11:26
→ TobyH4cker: 翻翻書很快就解決了 04/10 11:37
推 sprian: while沒離開的條件,永遠不會跑到fclose(),檔案當然沒東 04/10 15:48