我看了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.
我更喜欢“获取”“readline”的原因是因为能够将字符流提取为“streambuf”。
答案 0 :(得分:0)
我不太明白你的问题。
在msdn网站上,对于get函数,它说:
在所有情况下,分隔符既不从流中提取也不由函数返回。相反,getline函数提取但不存储分隔符。 在所有情况下,分隔符既不从流中提取也不由函数返回。相反,getline函数提取但不存储分隔符。
http://msdn.microsoft.com/en-us/library/aa277360(VS.60).aspx
我不认为你会遇到问题,因为msdn网站告诉分隔符既不是从流中提取的,也不是通过函数返回的。
或许我在这里错过了一点?
答案 1 :(得分: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字节,则只是一个示例。