我正在嘗試查找並重新讀取數據。但代碼失敗。ifstream有什麼問題seekg
的代碼是
std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary);
std::streampos pos = ifs.tellg();
std::cout <<" Current pos: " << pos << std::endl;
// read the string
std::string str;
ifs >> str;
std::cout << "str: " << str << std::endl;
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// seek to the old position
ifs.seekg(pos);
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// re-read the string
std::string str2;
ifs >> str2;
std::cout << "str2: (" << str2.size() << ") " << str2 << std::endl;
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
我的輸入測試文件是
qwe
產量爲
Current pos: 0
str: qwe
Current pos: 3
Current pos: 0
str2: (0)
Current pos: -1
誰能告訴我有什麼不對?
可能重複[seekg()函數將失敗(http://stackoverflow.com/questions/11264764/seekg-function-fails) – amo 2014-09-16 23:00:33