Look at this small code, it open a ifstream :
std::ifstream _fcs;
bool openFile(char* path)
{
istream::pos_type pos;
int tmp = 0;
_fcs.open(path, fstream::binary | fstream::in);
if(!_fcs.is_open())
return开发者_Go百科 false;
tmp = 0;
pos = 0x404;
_fcs.seekg(0x404);
pos = _fcs.tellg(); /// return zero
_fcs >> tmp; ///
_fcs.read((char*)&tmp, 4);
return true;
}
i have two problems.
- after seekg, tellg return zero and when I read data it reads from beginning of file.
- operator >> seems doesn't work. always return zero!
////------------------------------------------------
thanks for your attentions. I found a crazy solution, but I get confused! if I call seekg two times, it works, see this code: bool openFile(char* path)
{
istream::pos_type pos;
int tmp;
bool fail;
_fcs.open(path, fstream::binary | fstream::in);
if(!_fcs.is_open())
return false;
_fcs.seekg(0x402);
_fcs.seekg(0x402); /// When it comments, the tellg returns 0. am i crazy!?
fail = _fcs.fail();
assert(!fail);
pos = _fcs.tellg(); /// return 0x402!!!
/// _fcs >> tmp;
_fcs.read((char*)&tmp, 4);
return true;
}
really, what's happened?
////------------------------------------------------please help me...
thanks in advanced.Check the failbit using _fcs.fail()
after your seekg
call to make sure you haven't specified an invalid file position.
To double check the size use
_fcs.seekg(0,ios::end);
int length = _fcs.tellg();
You also need to use .read()
to get the len value, as your file is binary
In binary mode, >> is not supposed to work, you have to use ostream::write.
Does your file actually exists and have a size ? if note, you can't "move" to an arbitrary point in an empty file.
精彩评论