Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fstream unix problem in reading

Tags:

c++

unix

fstream

I am trying to read from binary file on UNIX. The file exists and has several data information in it.

The code looks like this:

fstrean fstrHandler;

string strFileName;

char Buf[30000];

fstrHandler.open(strFileName.c_str(), ios::in | ios::binary);

fstrHandler.seekp(0, std::ios_base::beg);

std::cout<< "Posi before read= "<< fstrHandler.tellg()<<endl; //*** Show after running 0

fstrHandler.read (Buf, 400);

std::cout<< "Posi after read= "<< fstrHandler.tellg()<<endl; //*** Show after running 0

std::cout<< " gcount ()= "<< fstrHandler.gcount ()<< << endl; //*** Show after running 0

if (fstrHandler.eof ()) {
       fstrHandler.clear();
}

After the read I get that the position in file is still zero zero, but the file is not empty.

like image 650
Boris Raznikov Avatar asked Dec 30 '25 07:12

Boris Raznikov


1 Answers

Try seekg rather than seekp, and is there 400 bytes in the file? this appears to work okay for me, if you input a file that contains more than 400 bytes. If less, then the tellg after read reports -1, but gcount() is correct.

Also, after opening the file - test to see if the file was indeed opened e.g.

if (fstrHandler)
{
// do stuff
}
else
  std::cerr << "foo bar" << std::endl;
like image 66
Nim Avatar answered Jan 01 '26 21:01

Nim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!