c++eof和故障位在读取二进制文件的过程中
c++ eof and failbit in the middle of reading binary file
我在读取二进制文件时遇到问题。它似乎没有读到尽头:
// get file size
ifs.open (inFile.c_str(), ios::binary | ios::ate);
cout << "file size: " << ifs.tellg() << endl;;
ifs.close();
// read file
ifs.open (inFile.c_str(), ios::in | ios::binary);
int counter = 0;
char c = 0;
for (counter = 0; ifs; ++counter)
ifs >> c;
cout << "last char: " << int(c) << endl;
cout << "read bytes: " << counter << endl;
cout << "fail? " << (ifs.fail() ? "yes" : "no") << endl;
cout << "bad? " << (ifs.bad() ? "yes" : "no") << endl;
cout << "eof? " << (ifs.eof() ? "yes" : "no") << endl;
ifs.close();
以下是输出。我不明白为什么我在文件中间得到eofbit
,以及为什么它与failbit
:一起出现
file size: 289384
last char: 1
read bytes: 288598
fail? yes
bad? no
eof? yes
我在Unix系统上得到了这一点
我做了一个测试,发现了问题。一旦将ofs << c
添加到for循环中,就很明显了。
它不是在读空白。
您可以通过添加#include <iomanip>
和ifs >> noskipws
或使用二进制输入函数(如ifs.get(c)
)来修复此问题
相关文章:
- 如何从二进制文件中读取字符串
- 从二进制文件中读取整数数组
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- C++:实际上不是从二进制文件中读取
- 如何从二进制文件中的给定符号中获取调用程序图
- 将内部带有矢量的结构保存/读取到二进制文件中
- 如何使用位字段将数据从二进制文件复制到结构中?
- C++中读/写二进制文件
- 对在不同二进制文件中创建的对象文件的依赖关系
- 将XSD文件嵌入一个(二进制文件)中
- 如果在执行过程中替换二进制文件,"const"数组是否驻留在内存中?
- 未显式引用对象的全局对象构造函数在最终二进制文件 - LD 中被丢弃
- 霍夫曼压缩读取文件不会复制二进制文件c++中的所有字节
- 在二进制文件c++中写入和加载结构向量
- 输出一个写在二进制文件c++中的double
- 我想在写入过程完成后从二进制文件中读取数据,也可以在没有写入过程的情况下读取数据
- 替换二进制文件c++中的字节序列
- 在二进制文件 c++ 中读取和写入字符串
- c++eof和故障位在读取二进制文件的过程中
- 在二进制文件C++中写入/读取字符串