0
可能重複:
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://stackoverflow.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