On 5 avr, 15:25, "barcaroller" <barcarol...@music.netwrote:
I have a text file with mixed carriage returns ('\n' and '\r\n').
On Linux, both the std::string getline() global function and
the std::iostream getline() member function are keeping some
of the newlines in the result (I suspect they look only for
the '\n').
Technically, it's implementation defined. Typically, however,
yes: Unix implementations treat a single 0x0A in the stream as a
newline; Windows implementations treat either a single 0x0A or
the sequence 0x0D, 0x0A as a newline.
Most of the time, this should not be a problem. In all of the
usual encodings (at least outside of the mainframe world), the
0x0D will result in an '\r' under Unix (and probably also under
Windows, if it isn't immediately followed by a 0x0A). In the
"C" locale, and probably in all other locales, '\r' is
whitespace. So it ends up ignored with the rest of the trailing
whitespace. (The one exception is C and C++ source code; for
some reason, the standard doesn't consider '\r' as whitespace in
source code.)
* Is there a quick way I can tell either function to gobble
up both Windows-style and Unix-style newlines?
Is there ever a need to?
* If not, what would be an efficient way of getting rid of
them? Currently I use string::find_last_of("\n\r") +
string::erase() but this is not very efficient.
I'd use an external program (e.g. tr). In practice, if a file
is on a shared file system, and thus being read by both Windows
and Unix, it's generally best (pragmatically, at least) to stick
with the Unix conventions.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34