开发者

istream get method behavior

I read istream::get and a doubt still hangs. Let's say my delimiter is actually the NULL '\0' character, what happens in this case? From what I read:

If the delimiting character is found, it is not extracted from the input sequence and remains as开发者_JAVA技巧 the next character to be extracted. Use getline if you want this character to be extracted (and discarded). The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.

The reason I would prefer "get" over "readline" is because of the capability to extract the character stream into a "streambuf".


I dont' quite get your problem.

On the msdn website, for the get function, it says:

In all cases, the delimiter is neither extracted from the stream nor returned by the function. The getline function, in contrast, extracts but does not store the delimiter. In all cases, the delimiter is neither extracted from the stream nor returned by the function. The getline function, in contrast, extracts but does not store the delimiter.

http://msdn.microsoft.com/en-us/library/aa277360(VS.60).aspx

I don't think your going to have a problem, since the msdn site tells that the delimiter is neither extracted from the stream, nor returned vy the function.

Or maybe I'm missing a point here?


If you have something like this, then delimiter will not get stuck in the input stream:

std::string read_str(std::istream & in)
{
        const int size  = 1024;
        char pBuffer[size];
        in.getline(pBuffer, size, '\0');
        return std::string(pBuffer);
}

just an example if you have '\0' as delimiter and strings are not bigger than 1024 bytes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜