我正在尝试编写一个程序来打印最后一行文件,我想出了以下内容。我在文件中执行SEEKs
的位置,但此代码在无限循环中运行。如果我发表评论(1)
并启用(2)
,则代码可以正常运行。我无法弄清楚原因。
#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)
答案 0 :(得分:3)
您需要在再次阅读之前清除流上的错误状态:
ifile.clear();
否则,第一次遇到EOF时,流会进入错误状态,所有后续读取都将失败。
请注意,如果您这样做并且您的文件只包含1(或0)行,那么您当前形式的代码将永远循环。
答案 1 :(得分:0)
最后的工作代码如下,问题可以通过以下方式之一解决。
#include <iostream>
#include <fstream>
int main()
{
std::string line;
int count = 0;
long int seek_length = -100l;
std::ifstream ifile("D:\\cprog\\test.csv");
#if 0
while(true){
seek_length *= 2;
count = 0;
ifile.seekg(seek_length, std::ios_base::end);
if( ifile.tellg() < 0 ){
ifile.clear();
ifile.seekg(0l, std::ios_base::beg);
while(std::getline(ifile,line));
break;
}
else
{
while(std::getline(ifile,line)){
++count;
}
if(count > 1)
break;
}
ifile.clear();
}
#else
char ch;
ifile.seekg(0l, std::ios_base::end);
do
{
ch = ifile.peek();
ifile.seekg(-1l, std::ios_base::cur);
std::cout << ch <<'~' << ifile.tellg() <<'\n';
}while(ch != '\n' && ifile.tellg() > -1 );
if(ifile.tellg() < 0 )
ifile.seekg(0l, std::ios_base::beg);
else
ifile.seekg(2l, std::ios_base::cur);
ifile.clear();
std::getline(ifile,line);
#endif
if(line.empty())
std::cout<<"------- EMPTY LINE -----\n";
else std::cout << line << '\n';
}