While normally I'd agree with loctrice that using
std::getline
is a good idea, doing what you want is a real pain using
std::getline
across all types of streams. FREX windows consoles can only be put into a end of file state by entering a
CTRL^Z
at a new line while for files it can be anywhere and not always after a newline is entered as on the console.
So what can you do about it? Well rolling your own isn't too hard, especially if you use what's already in the C++ standard library.
std::istream &read_line_with_newline( std::istream &str, std::string &line )
{
for( char next = 0; str && next != '\n'; )
{
str >> std::noskipws >> next;
line += next;
}
return str;
}
It's a bit of a flashback to pre-C++ days when you'd use fgetchar to do this sort of thing. The noskipws manipulator means that when you're reading don't ignore whitepace, process them as ordinary characters.
Edited for English fail and again for a typo, anyone'd think it wasn't my mother tongue!