看看这个小代码,它打开了一个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两次,它会工作,请参见下面的代码:
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;
}真的,发生什么事了?
////
请帮帮我..。
谢谢你的进阶。
发布于 2011-05-05 04:36:37
在_fcs.fail()调用后使用seekg检查故障位,以确保没有指定无效的文件位置。
要重复检查大小使用
_fcs.seekg(0,ios::end);
int length = _fcs.tellg();您还需要使用.read()来获取len值,因为您的文件是二进制的。
https://stackoverflow.com/questions/5892348
复制相似问题