为什么当我提取到“char”和“int”时,IOStream 的 EOF 标志行为存在差异?

发布于 2024-12-13 21:15:46 字数 650 浏览 5 评论 0原文

最近,我在软件中遇到了一个错误,该错误是由 stringstream 对象引起的,该对象在我预期之前设置了 EOF 标志。尽管我设法找出发生了什么,但我无法找出为什么会发生这种情况。一个例子:

stringstream test ("a b");
char temp, temp2;

test >> temp >> temp2;
cout << "eof: " << test.eof() << endl;  

运行时,这显示:

eof: 0

这是我期望的输出。 (当我尝试再次读取某些内容时,我希望字符串流将 EOF 标志设置为 1)

但是,当我对上面的示例进行小更改时:

stringstream test ("4 2");
int temp, temp2;

test >> temp >> temp2;
cout << "eof: " << test.eof() << endl;

输出显示:

eof: 1

为什么在这种情况下设置 EOF 标志,但是不是在上一篇中吗?

Recently I've come across a bug in my software that was caused by a stringstream object that had it's EOF flag set before I expected it. Even though I managed to found out what happened, I was not able to find out why this is happening. An example:

stringstream test ("a b");
char temp, temp2;

test >> temp >> temp2;
cout << "eof: " << test.eof() << endl;  

When run, this shows:

eof: 0

This is the output I would expect. (I would expect the stringstream to set the EOF flag to 1 when i attempt to read something again)

However, when I make a small change to the above example:

stringstream test ("4 2");
int temp, temp2;

test >> temp >> temp2;
cout << "eof: " << test.eof() << endl;

the output shows:

eof: 1

Why does the EOF flag get set in this situation, but not in the previous one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-12-20 21:15:46

当您一次提取一个字符时,流无法知道已到达 EOF,直到尝试提取不存在的字符。

但是,当您将格式化数据提取为 int 等类型时,解析器会尝试从流中提取尽可能多的字符来形成数字; “尝试提取不存在的字符”部分将作为此过程的一部分发生(实际上是最终的“迭代”),因此可以设置 EOF。

When you're extracting one character at a time, the stream can't know that the EOF has been reached until you try to extract a character that isn't there.

But when you're extracting formatted data into a type like int, the parser attempts to pull as many characters out of the stream as it can to form the number; the "tries to extract a character that isn't there" part will occur as part of this process (the final "iteration", in fact), so EOF can be set.

此生挚爱伱 2024-12-20 21:15:46

operator>> 默认情况下会跳过空白字符,因此第一次读入 char 时将读取 a,第二次将跳过 并读取 b,第三个将到达字符串末尾并失败,设置 eof 标志。

int 情况下,解析 int 时可以读取多个字符,因为 int 可能由多个数字表示。读取整数时,将在读取 2 后进行第二次读取尝试。尽管 int 的读取将会成功,但这将为流设置 eof 标志。

这就是为什么您应该检查 !fail() 而不是 good() 来查看读取操作是否成功以及为什么将流转换为 bool code>(或 C++03 中的 void*)也使用 !fail()

operator>> skips whitespace characters by default, so the first read into a char will read a, the second will skip and read b, a third would reach the end of the string and fail, setting the eof flag.

In the int case, multiple characters can be read while parsing an int because an int may be represent by multiple digits. While reading the integer a second read attempt will be made after reading the 2. This will set the eof flag for the stream although the read of the int will succeed.

This is why you should check !fail() and not good() to see if a read operation succeeded and why the conversion of a stream to bool (or void* in C++03) also uses !fail().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文