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.

Thomas
  • 2,939
  • 6
  • 32
  • 29
  • 2
    You can use a facet. Jerry Coffin presented a great example of how to do this in an answer to another question: http://stackoverflow.com/questions/1567082/how-do-i-iterate-over-cin-line-by-line-in-c/1567703#1567703 – James McNellis May 13 '10 at 02:42
  • 4
    Why do you not want to use `std::getline()`? – James McNellis May 13 '10 at 02:44

1 Answers1

5

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
  • 57,473
  • 20
  • 96
  • 131