到达文件末尾后倒回 ifstream 对象
Rewind an ifstream object after hitting the end of file
有一个包含几个字符(比如 10 个)的文本文件,您可以尝试从中读取 1000 个字符。
char *buf = new char[1000];
ifstream in("in.txt");
in.read(buf, 1000);
当然,这也会设置 eofbit flag (and the failbit),但是,您将能够获得所需的字符。
现在,假设您想再次读取文件(从头开始):
in.seekg(0); // Sets input position indicator.
in.read(buf, 100); // Try to read again.
这不起作用:因为如果您调用:
int count = in.gcount() // Charecters readed from input.
你会注意到 count == 0
。这意味着它根本没有读取任何内容。
因此出现问题:到达文件末尾后如何倒回文件?
解决方案
使用 clear 在调用 seekg
之前清理 ifstream 的状态。 一定要先检查,如果没有,以后需要知道状态。
in.clear();
in.seekg(0);
说明
seekg sets the cursor position, but doesn't clear state bit failbit so, the ifstream 实例“认为”有问题。
来自标准规范:
std::basic_istream::seekg behaves as UnformattedInputFunction, except that gcount() is not affected.
我们可以读入 UnformattedInputFunction:
The following standard library functions are UnformattedInputFunctions:
basic_istream::seekg, except that it first clears eofbit and does not modify gcount
在问题示例中,如果您打印 seekg 前后的状态,您将得到:
cout << "State before seekg: " << in.rdstate() << endl; // Prints 3 (11 in binary) failbit and eofbit activated.
in.seekg(0);
cout << "State after seekg: " << in.rdstate() << endl; // Prints 2 (10 in binary) just failbit activated.
这就是原因!!
seekg 不清除 failbit 并且由于某些实现原因,它不适用于激活的此类位。
我的猜测
为什么 failbit 激活后 seekg
不起作用?
这与这个位不仅在流到达文件末尾时被激活有关。并且可能在激活 failbit 后,使用 seekg
容易出错或可能显示未定义的行为。
有一个包含几个字符(比如 10 个)的文本文件,您可以尝试从中读取 1000 个字符。
char *buf = new char[1000];
ifstream in("in.txt");
in.read(buf, 1000);
当然,这也会设置 eofbit flag (and the failbit),但是,您将能够获得所需的字符。
现在,假设您想再次读取文件(从头开始):
in.seekg(0); // Sets input position indicator.
in.read(buf, 100); // Try to read again.
这不起作用:因为如果您调用:
int count = in.gcount() // Charecters readed from input.
你会注意到 count == 0
。这意味着它根本没有读取任何内容。
因此出现问题:到达文件末尾后如何倒回文件?
解决方案
使用 clear 在调用 seekg
之前清理 ifstream 的状态。 一定要先检查,如果没有,以后需要知道状态。
in.clear();
in.seekg(0);
说明
seekg sets the cursor position, but doesn't clear state bit failbit so, the ifstream 实例“认为”有问题。
来自标准规范:
std::basic_istream::seekg behaves as UnformattedInputFunction, except that gcount() is not affected.
我们可以读入 UnformattedInputFunction:
The following standard library functions are UnformattedInputFunctions:
basic_istream::seekg, except that it first clears eofbit and does not modify gcount
在问题示例中,如果您打印 seekg 前后的状态,您将得到:
cout << "State before seekg: " << in.rdstate() << endl; // Prints 3 (11 in binary) failbit and eofbit activated.
in.seekg(0);
cout << "State after seekg: " << in.rdstate() << endl; // Prints 2 (10 in binary) just failbit activated.
这就是原因!!
seekg 不清除 failbit 并且由于某些实现原因,它不适用于激活的此类位。
我的猜测
为什么 failbit 激活后 seekg
不起作用?
这与这个位不仅在流到达文件末尾时被激活有关。并且可能在激活 failbit 后,使用 seekg
容易出错或可能显示未定义的行为。