Originally Posted by swoopy
Originally Posted by swoopy
getline is portable. The way it appears to us is perfectly transparent.
std::endl is also portable. So go ahead and use these two freely.
However the escape characters are single character tokens. '\n' is not the same thing in Linux and windows. Well... it is. It still represents a new line. However, under windows what one wants to represent a new line is '\r\n', while under Linux it is just '\n'.
So, in all truth, if you wanted to search for new lines using escape characters, '\n' would be the one. Not '\r'. Because '\n' happens in both systems.
Regardless, the rules are somewhat simple:
- getline is the best way to extract a line from a stream. It's portable across systems.
- std::endl is the best option to input a new line. It's portable across systems.
And one last thing... '\n' is portable if the file is opened in text mode using a filestream. So, if you write to a ofstream using '\n', it will be automagically converted to '\r\n' under windows and remain '\n' under Linux.
EDIT: I'm so glad we aren't talking about \c anymore :)
> string::size_type pos = contains.find("\\c");
And of course the same applies here:
And with the replace. So:Code:
string::size_type pos = contains.find("\r");
> contains.replace(pos, 2, "\r");
contains.replace(pos, 1, "\r");
>> However, say a customer wanted to search for a carriage return following a word, such as "Line 1\c"
If you specifically want to allow the user to search for a carriage return following a word, I believe that requires you to read the file in binary using unformatted input, otherwise the carriage return will be converted to a newline. If you don't mind making them search for a generic platform independent newline, then you can use formatted input like getline and forget about the '\r' and '\c' stuff.
Yup, I am using binary mode to read the line.Quote:
Originally Posted by Daved
However if you search for carriage return you will not find it under linux unless someone explicitly put it there. Search for new line '\n' if what you want is to look for a new line under both systems.
From what I gathered so far you don't seem to want to look specifically for a carriage return. you want to look generally for a new line. The best way, ensuring portability, is to use an oftsream. Or, at the very least, use getline as anon suggested before.
Perhaps I'm not explaining correctly - but I do actually want the actual carriage return (0xD) character. I have a function that does newline characters, now I just need it to do carriage return (and only the carriage return character, nothing more).Quote:
Originally Posted by Mario F.
I gave all the examples using newline because I figure that would be the most common case. But I seem to have confused everyone :(. Sorry about that :P.
No worries mate. you got it already. have fun