我想要打印出文本文件的最後10行。與此程序我已經能夠讀取整個文本文件,但我不知道如何處理文本文件保存的數組,如何幫助?打印出文件的最後10行
// Textfile output
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int i=1;
char zeile[250], file[50];
cout << "filename:" << flush;
cin.get(file,50); ///// (1)
ifstream eingabe(datei , ios::in); /////(2)
if (eingabe.good()) { /////(3)
eingabe.seekg(0L,ios::end); ////(4)
cout << "file:"<< file << "\t"
<< eingabe.tellg() << " Bytes" ////(5)
<< endl;
for (int j=0; j<80;j++)
cout << "_";
cout << endl;
eingabe.seekg(0L, ios::beg); ////(6)
while (!eingabe.eof()){ ///(7)
eingabe.getline(zeile,250); ///(8)
cout << setw(2) << i++
<< ":" << zeile << endl;
}
}
else
cout <<"dateifehler oder Datei nicht gefunden!"
<< endl;
return 0;
}
雖然你可能已經知道這一點,UNIX [尾巴](http://en.wikipedia.org/wiki/Tail_(UNIX))命令究竟是幹什麼的你要。 – darioo 2011-04-26 18:45:45
@darioo:這個問題聽起來像作業。 – 2011-04-26 18:57:33
順便說一句,如果你尋找文件的末尾,然後向後尋找你的起點,而不是解析整個文件,你會發現它在更大的文件上快得多**。 – 2011-04-26 18:59:58