开发者

C++ STL - detecting reaching end of binary file [duplicate]

开发者 https://www.devze.com 2023-03-08 15:29 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why does std::fstream set the EOF bit the way it does?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why does std::fstream set the EOF bit the way it does?

hello

iam loading a binary file using rea开发者_运维百科d(...), however, the eof() gets never true so i check for file end using gcount(), which obviosly is plain wrong

how to detect eof() of a binary file properly?

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

}   


I've found an article discussing just that.

0

精彩评论

暂无评论...
验证码 换一张
取 消