c++ getline reads entire file in Windows

后端 未结 2 1241
别那么骄傲
别那么骄傲 2021-01-24 01:44

This looks like a similar question to this one, however I think my case might actually be a bit different. The code is as below:

void readOmronResults(string fil         


        
相关标签:
2条回答
  • 2021-01-24 02:34

    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.

    0 讨论(0)
  • 2021-01-24 02:37

    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.

    0 讨论(0)
提交回复
热议问题