我正在嘗試編寫一個程序來打印最後一行文件,並且我想出了以下內容。我在文件中的位置SEEKs
,但是此代碼在無限循環中運行。如果我註釋掉(1)
並啓用(2)
,則代碼正常工作。我無法弄清楚原因。在ifstream getlines [耗盡文件]後seekg問題
#include <iostream>
#include <fstream>
int main()
{
std::string line;
int count = 0;
long int seek_length = -1l;// should be -100l
std::ifstream ifile("D:\\cprog\\test.csv");// --(1)
while(true){
seek_length *= 2;
count = 0;
//std::ifstream ifile("D:\\cprog\\test.csv"); //-- (2)
ifile.seekg(seek_length, std::ios_base::end);
while(std::getline(ifile,line)){
++count;
}
if(count > 1)
break;
}
std::cout << line << '\n';
}
編譯:G ++(GCC)4.9.2(MINGW)
可怕的標題。讓標題描述問題。 –