C++ STL - 检测二进制文件的到达端
问题描述:
可能重复:
Why does std::fstream set the EOF bit the way it does?C++ STL - 检测二进制文件的到达端
你好
IAM装载使用二进制文件中读取(...),然而, eof()永远不会是真的 所以我检查文件结束使用gcount(),这明显是错误的
如何正确检测二进制文件的eof()?
std::ifstream rf;
rf.open(fpath.c_str(), std::ios::in | std::ios::binary);
while(!rf.eof()) {
std::string objectName;
READ_STR_FROM_BINFILE(rf, objectName);
//macro code : {
// size_t len;
// ff.read(reinterpret_cast<char *>(&len), sizeof(len));
// std::vector<char> v(len);
// ff.read(&v[0], len);
// ss = std::string(v.begin(), v.end());
//}
if (rf.gcount() == 0) {
//if this happens, there is nothing more to read
//but the strange thing is, .eof() is not true at this point
break;
}
//loading some structures here
}
请参阅[为什么std :: fstream设置EOF位的方式?](http://*.com/questions/1039667/why-does-stdfstream-set-the-eof-bit-the-方式-IT-一样)。 – Johnsyweb 2011-05-27 11:46:19
''应该** **应该工作,但是请注意,只要设置了任何错误标志,它就不会被达到:因此:不要只测试eof,测试任何错误标志。 – 2011-05-27 11:46:56