看这个小的代码,它打开一个ifstream的:问题与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 false;
tmp = 0;
pos = 0x404;
_fcs.seekg(0x404);
pos = _fcs.tellg(); /// return zero
_fcs >> tmp; ///
_fcs.read((char*)&tmp, 4);
return true;
}
我有两个问题。
- seekg后,所以tellg返回零,当我读数据从文件的开头读取。
- 运营商>>似乎不起作用。总是归零!
//// ---------------------------------------- --------
感谢您的关注。我发现了一个疯狂的解决方案,但我感到困惑! 如果我叫seekg两次,它的工作原理,看到这种代码:
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;
}
真的,发生了什么?
//// -------------------------------------------- ----
请帮我...
感谢先进。
是的,它的存在。这是一个二进制文件,我也用十六进制编辑器打开它。我想读不写! :( – Mostafa 2011-05-05 03:42:29
对不起,误读。使用istream :: read然后。 – 2011-05-05 03:46:07
我使用它,但为什么seekg没有改变get指针!!!! – Mostafa 2011-05-05 03:52:48