打印出文件的最后 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;
}
I want to have the option to print out the last 10 lines of a textfile . with this program I've been able to read the whole textfile, but I can't figure out how to manipulate the array in which the textfile is saved, any help?
// 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在这里研究了建议的方法,并在我的博客文章中描述了所有内容。有一个更好的解决方案,但您必须跳到最后并保留所有需要的行:
I investigate proposed approaches here and describe all in my blog post. There is a better solution but you have to jump to the end and persist all needed lines:
试试这个:
// 一个知道如何使用运算符 >> 读取一行的类
// 一个循环缓冲区,仅保存最后 n 行。
// 主要的
Try this:
// A class that knows how to read a line using operator >>
// A circular buffer that only saves the last n lines.
// Main
基本上,您不会将文件内容保存到任何数组中。以下示例将为您提供一个良好的开端:
避免将整个文件存储到内存中可能是更明智的做法,因此您可以通过这种方式重写最后 2 个段:
Basically, you are not saving the file contents to any array. The following sample will give you a head start:
It would probably be wiser to avoid storing the whole file into memory, so you could re-write the last 2 segments this way:
eof() 函数并没有做你和其他一百万个 C++ 新手所认为的那样的事情。它不会预测下一次读取是否有效。在 C++ 中,与任何其他语言一样,您必须检查每个读取操作的状态,而不是读取之前输入流的状态。所以规范的 C++ 读取行循环是:
另外,您应该读入
std::string
,并去掉 250 值。The eof() function does not do what you and it seems a million other C++ newbies think it does. It does NOT predict if the next read will work. In C++ as in any other language, you must check the status of each read operation, not the state of the input stream before the read. so the canonical C++ read line loop is:
Also, you should be reading into a
std::string
, and get rid of that 250 value.创建一个具有 10 个槽的循环缓冲区,并在读取文件行时将它们放入该缓冲区中。完成 thr 文件后,执行position++ 转到第一个元素并将其全部打印。
如果文件少于 10 行,请注意空值。
Do a circular buffer with 10 slots and while reading the file lines, putting them into this buffer. When you finish thr file, do a position++ to go to the first element and print them all.
Pay attention for null values if the file has less than 10 lines.
重复步骤3和4,直到文件读取完毕。
Repeate step 3 and 4 till the file is finished reading.