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 对象
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01