title: C++ STL - detecting reaching end of binary file
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1039667/why-does-stdfstream-set-the-eof-bit-the-way-it-does">Why does std::fstream set the EOF bit the way it does?</a>  </p>
  </blockquote>
  
  
  
  <p>hello</p>
  
  <p>iam loading a binary file using read(...), however, the eof() gets never true
  so i check for file end using gcount(), which obviosly is plain wrong</p>
  
  <p>how to detect eof() of a binary file properly?</p>
  
  <pre><code>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&lt;char *&gt;(&amp;len), sizeof(len)); 
      //  std::vector&lt;char&gt; v(len); 
      //  ff.read(&amp;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
  
  }   
  </code></pre>