tags:

views:

128

answers:

2
+1  Q: 

c++ file bad bit

Hi, when I run this code, the open and seekg and tellg operation all success. but when I read it, it fails, the eof,bad,fail bit are 0 1 1.

What can cause a file bad? thanks


int readriblock(int blockid, char* buffer)
{
   ifstream rifile("./ri/reverseindex.bin", ios::in|ios::binary);

   rifile.seekg(blockid * RI_BLOCK_SIZE, ios::beg);
   if(!rifile.good()){ cout<<"block not exsit"<<endl; return -1;}
   cout<<rifile.tellg()<<endl;

   rifile.read(buffer, RI_BLOCK_SIZE);

   **cout<<rifile.eof()<<rifile.bad()<<rifile.fail()<<endl;**

   if(!rifile.good()){ cout<<"error reading block "<<blockid<<endl; return -1;}

   rifile.close();
   return 0;
}

+1  A: 

try old errno. It should show real reason for error. unfortunately there is no C++ish way to do it.

Andrey
+1  A: 
Josh Kelley
Thanks everyone, It turns out my buffer is not writable when I use this function.