开发者

istream stop at \n

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开发者_JS百科 stream?

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜