C++STL-检测二进制文件的末尾

C++ STL - detecting reaching end of binary file

本文关键字:二进制文件 检测 C++STL-      更新时间:2023-10-16

可能重复:
为什么std::fstream会以这种方式设置EOF位?

你好

iam使用read(…)加载二进制文件,但是,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
}   

我发现一篇文章正在讨论这个问题。