为何将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
相关文章
相关标签/搜索