简体   繁体   English

为什么不搜索(0)清除流的eof状态?

[英]Why does not seekg(0) clear the eof state of stream?

I would like to know if and why seekg(0) is not supposed to clear the eofbit of a stream. 我想知道是否以及为什么seekg(0)不应该清除流的eofbit I am in a point where I have already read all the stream, thus EOF has been reached (but no failbit is set yet) and want to go back with seekg() to a valid position and read some chars again. 我已经阅读了所有的流,因此已达到EOF (但尚未设置failbit )并且想要将seekg()到有效位置并再次读取一些字符。 In this case seekg(0) seems "to work" with the eofbit set, but as soon as I try to read from the stream, the failbit is set. 在这种情况下, seekg(0)似乎与eofbit集合“工作”,但是一旦我尝试从流中读取,就会设置failbit。 Is this logic, correct or is my implementation bad? 这是逻辑,正确还是我的实施不好? Am I supposed to recognize this case and clear the eofbit manually (if the failbit is not set)? 我是否应该认识到这种情况并手动清除eofbit(如果未设置failbit)?

EDIT: 编辑:

The following program provided by a reader gives different results in my implementation ( mingw32-c++.exe (TDM-2 mingw32) 4.4.1 ): 读者提供的以下程序在我的实现中给出了不同的结果(mingw32-c ++。exe(TDM-2 mingw32)4.4.1):

#include <sstream>
#include <iostream>
#include <string>

int main() {
        std::istringstream foo("AAA");
        std::string a;
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
        foo.seekg(0);
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1
}

The comments above are from the user who tried that program in his implementation. 以上评论来自在其实施中尝试该程序的用户。 I obtain these results: 我得到了这些结果:

1 0
1 0
1 1
1 1

According to the new standard clear() is supposed to reset the eofbit (§ 27.7.2.3): 根据标准, clear()应该重置eofbit (§27.7.2.3):

basic_istream<charT,traits>& seekg(pos_type pos);

Effects: Behaves as an unformatted input function ..., except that the function first clears eofbit ... 效果:作为无格式输入函数...,除了该函数首先清除eofbit ...

But in the old standard (§ 27.6.1.3) there is no mention of clearing the eofbit ! 但是在标准(§27.6.1.3)中没有提到清除eofbit

And a simple test: 一个简单的测试:

#include <sstream>
#include <iostream>
#include <string>

int main() {
        std::istringstream foo("AAA");
        std::string a;
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
        foo.seekg(0);
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
        foo >> a;
        std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1
}

Why not just manually clear() the stream then go back once the eofbit has been set? 为什么不只是手动清除()流然后在设置eofbit后返回? EOF has been reached, why should seekg clear it automatically? 已达到EOF,为什么要自动清除? Doing that would seem to cause more problems. 这样做似乎会导致更多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM