Reputation: 627
I need to read a txt file created in Windows in C++ program, compiled on the Debian Linux. Unfortunately I have a problem with the end of line delimiters. I know that the end of the line indicators are different in Linux and Windows. Consequently, in Linux my C++ program reads something like "correct_line^M".
My question: How can I read in Linux my file created in Windows, correctly? Do I need to convert it manually to Linux representation (I would like to avoid it)?
Thank you.
Upvotes: 1
Views: 978
Reputation: 153929
You'll have to do it yourself. (IMNO, a good library would do it
automatically, in filebuf
, if opened in text mode. But the libraries
I'm familiar with don't.)
Depending on what you're doing, it may not matter. Any line oriented input should accept trailing white space anyway, and the extra 0x0D character is white space. So except for editors, it usually won't matter.
If you want to suppress the extra 0x0D when writing the file (under Windows), just open the file in binary mode. For that matter, when portability of the file is a concern, it's often a good idea to open the file in binary mode, and write whichever convention the protocol requires. (Using the two character sequence 0x0D, 0x0A is more or less standard, and is what most Internet protocols specify.) When reading, again, open in binary mode, and write your code to accept any of the usual conventions: the two character sequence 0x0D, 0x0A, or either a single 0x0D or a single 0x0A. (This could be done with a filtering streambuf.)
Upvotes: 3