看這個小的代碼,它打開一個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