爲何將iostream :: eof放在循環條件(即`while(!stream.eof())`)內?

問題:

I just found a comment in this answer saying that using iostream::eof in a loop condition is "almost certainly wrong". 我剛剛在答案中找到一條評論,說在循環條件下使用iostream::eof是「幾乎確定是錯誤的」。 I generally use something like while(cin>>n) - which I guess implicitly checks for EOF. 我一般使用while(cin>>n)類的東西-我猜它隱式地檢查EOF。 ios

Why is checking for eof explicitly using while (!cin.eof()) wrong? 爲何顯式地使用while (!cin.eof())檢查eof是錯誤的? ide

How is it different from using scanf("...",...)!=EOF in C (which I often use with no problems)? 與在C語言中使用scanf("...",...)!=EOF有什麼不一樣(我常常會毫無問題地使用它)? oop


解決方案:

參考一: https://stackoom.com/question/NW9F/爲何將iostream-eof放在循環條件-即-while-stream-eof-內
參考二: https://oldbug.net/q/NW9F/Why-is-iostream-eof-inside-a-loop-condition-i-e-while-stream-eof-considered-wrong
相關文章
相關標籤/搜索