2
我有提示用戶輸入文件名的代碼,然後在控制檯上顯示文件內容。但是,我使用的printf()語句直到最後纔會打印出來,所以用戶不知道輸入文件名。printf打印不正確
int main(int argc, char * argv[]){
printf("%s", "What file would you like to open?\t");
char filename[100];
scanf("%[^\n]", filename);
printf("You chose:\t%s\n", filename);
return 0;
}
程序當前掃描控制檯,然後打印出printf()語句。 scanf()語句正常工作,只是不在正確的時間。任何想法我的問題是什麼?
編輯:從命令行運行時程序工作正常,但問題仍然存在於Eclipse中。
是與stdout相關的嗎?我認爲scanf使用stdin? – Krypton
我在程序中添加了該行,但它仍然不能在Eclipse中使用。 – nh0815
@ nh0815然後,你可以嘗試在你的printf之前添加'setvbuf(stdout,NULL,0,_IONBF)',看看它是否有效? – lccarrasco