开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜