tags:

views:

303

answers:

2

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 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".

A: 

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?

Danielle
So what's the usual way of dealing with the delimiter being stuck in the input stream? (can you tell I am a noob with C++ streams ;-)
jldupont
`.ignore(1)` seems appropriate.
Johannes Schaub - litb
+1 to "litb"... thanks.
jldupont
A: 

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.

AlexKR
Thanks AlexKR. I am looking for a solution based on the transfer of the input character stream into a `streambuf` which, if I understood correctly, takes care of the automatic resizing activity.
jldupont