Rewind an ifstream object after hitting the end of file(到达文件末尾后倒带 ifstream 对象)
问题描述
有一个包含几个字符(比如说 10 个)的文本文件,您可以尝试从中读取 1000 个字符.
Having a text file with a few characters (lets say 10), you can try to read 1000 characters from it.
char *buf = new char[1000];
ifstream in("in.txt");
in.read(buf, 1000);
这当然会设置 eofbit 标志(和failbit),但是,您将能够获得所需的字符.
This, of course, will set the eofbit flag (and the failbit too), however, you will be able to obtain the desired characters.
现在,假设您想再次读取文件(从头开始):
Now, suppose you want to read the file again (from the beginning):
in.seekg(0); // Sets input position indicator.
in.read(buf, 100); // Try to read again.
这不起作用:因为如果你打电话:
This does not work: because if you call:
int count = in.gcount() // Charecters readed from input.
你会注意到count == 0.这意味着它根本没有读取任何内容.
you will notice that count == 0. Meaning it has not read anything at all.
因此问题是:到达文件末尾后如何倒带文件?
Hence the question: How can you rewind the file after you get to the end of the file?
推荐答案
解决方案
使用 clear 在调用之前清除 ifstream 的状态 <代码>搜索代码>.如果您以后不需要知道状态,请务必先检查.
Solution
Use clear for cleaning the state of the ifstream before call seekg. Be sure to check first if you don't will need to know the state later.
in.clear();
in.seekg(0);
说明
seekg 设置光标位置,但不会clear 状态位 failbit 所以,ifstream 实例thinks"还是有问题.
Explanation
seekg sets the cursor position, but doesn't clear state bit failbit so, the ifstream instance "thinks" there is something wrong yet.
来自标准规范:
std::basic_istream::seekg 表现为 UnformattedInputFunction,除了 gcount() 不受影响.
std::basic_istream::seekg behaves as UnformattedInputFunction, except that gcount() is not affected.
我们可以阅读UnformattedInputFunction:
以下标准库函数是UnformattedInputFunctions:
basic_istream::seekg,除了它首先清除 eofbit 并且不修改 gcount
basic_istream::seekg, except that it first clears eofbit and does not modify gcount
在问题示例中,如果您在 seekg 之前和之后打印状态:
In the question example if you print the state before and after the seekg you get:
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 并且由于某些实施原因,它不适用于激活这样的位.
seekg doesn't clear failbit and for some implementation reason, it doesn't works with such bit activated.
为什么 seekg 在 failbit 时不起作用a> 是否被激活?
Why seekg does not work when failbit is activated?
这与这个位不仅在流到达文件末尾时才被激活有关.并且可能是在激活故障位后,使用 seekg 容易出错或可能显示未定义行为的情况.
It has to do with the fact this bit is not only activated when the stream reach the end of the file. And it might be situations in which after failbit is activated, using seekg is error prone or might shows undefined behaviour.
这篇关于到达文件末尾后倒带 ifstream 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:到达文件末尾后倒带 ifstream 对象
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
