c++ getline reads entire file in Windows

倾然丶 夕夏残阳落幕 提交于 2019-12-02 02:53:24

My operating system is Windows and according to my text editor the original input file is Mac-OS style.

Yes, this is the problem. Windows' C and C++ standard libraries will assume that text files use the Windows line ending, U+0D U+0A.

"Mac OS style" is an odd thing for the text editor to say, because the other line ending in common use U+0A which is common to the entire Unix family including Linux. Long ago, Mac OS used U+0D which makes the phrase "Mac OS style" ambiguous and anachronistic.

But if it is because of this, why is the original code (with the two lines commented out) able to give a correct result?

It didn't. Both versions of the program treat the file as if it contains one very long line.

If they are Mac OS endings '\r', according to the docs: http://www.cplusplus.com/reference/string/string/getline/ this behaviour is not surprising.

Paraphrasing the docs: when not giving a delimiter, getline will ready until a newline ('\n') character is encountered.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!