2

我读了istream::get并且怀疑仍然存在。假设我的分隔符实际上是 NULL '\0' 字符,在这种情况下会发生什么?从我读到的:

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.

我更喜欢“get”而不是“readline”的原因是能够将字符流提取到“streambuf”中。

4

2 回答 2

1

我不太明白你的问题。

在 msdn 网站上,对于 get 函数,它说:

在所有情况下,分隔符既不是从流中提取的,也不是由函数返回的。相比之下,getline 函数提取但不存储分隔符。在所有情况下,分隔符既不是从流中提取的,也不是由函数返回的。相比之下,getline 函数提取但不存储分隔符。

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

我认为您不会有问题,因为 msdn 站点告诉分隔符既不是从流中提取的,也不是通过函数返回的。

或者,也许我在这里遗漏了一点?

于 2009-11-05T15:26:50.170 回答
0

如果你有这样的东西,那么分隔符就不会卡在输入流中:

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

只是一个例子,如果你有 '\0' 作为分隔符并且字符串不大于 1024 字节。

于 2009-11-05T15:55:21.330 回答