views:

93

answers:

1

Is there any way of telling istream to keep going until it hits \n instead of normal white space and with out the use of getline and also keeping any format options in the stream?

Thanks.

+6  A: 

What's wrong with std::getline()? Anyway, how about writing your own function:

#include <iostream>
#include <iterator>

template<class In, class Out, class T>
Out copy_until(In first, In last, Out res, const T& val)
{
    while( first != last && *first != val ) *res++ = *first++;
    return res;
}

// ...    
std::string line;
copy_until(std::istreambuf_iterator<char>(std::cin),
           std::istreambuf_iterator<char>(),
           std::back_inserter(line), '\n');
std::cout << line << std::endl;

The function will consume the newline, but won't place it in the output.

wilhelmtell
Agreed. OP said no `getline()`, but that's really the way this is done....
jwismar