打印出文件的最后10行

print out the last 10 lines of a file

本文关键字:最后 10行 文件 打印      更新时间:2023-10-16

我想要打印出文本文件的最后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;
    }

试试这个:

#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

//知道如何使用operator>>

读取一行的类
struct Line
{
    std::string theLine;
    operator std::string const& () const { return theLine; }
    friend std::istream& operator>>(std::istream& stream, Line& l)
    {
        return std::getline(stream, l.theLine);
    }
};

//循环缓冲区,只保存最后n行。

class Buffer
{
    public:
        Buffer(size_t lc)
            : lineCount(lc)
        {}
        void push_back(std::string const& line)
        {
            buffer.insert(buffer.end(),line);
            if (buffer.size() > lineCount)
            {
                buffer.erase(buffer.begin());
            }
        }
        typedef std::list<std::string>      Cont;
        typedef Cont::const_iterator        const_iterator;
        typedef Cont::const_reference       const_reference;
        const_iterator begin()  const       { return buffer.begin(); }
        const_iterator end()    const       { return buffer.end();}
    private:
        size_t                      lineCount;
        std::list<std::string>      buffer;
};    

//主要
int main()
{
    std::ifstream   file("Plop");
    Buffer          buffer(10);
    // Copy the file into the special buffer.
    std::copy(std::istream_iterator<Line>(file), std::istream_iterator<Line>(),
            std::back_inserter(buffer));
    // Copy the buffer (which only has the last 10 lines)
    // to std::cout
    std::copy(buffer.begin(), buffer.end(),
            std::ostream_iterator<std::string>(std::cout, "n"));
}

基本上,您没有将文件内容保存到任何数组中。下面的示例将为您提供一个良好的开端:

#include <iostream>
#include <vector>
#include <string>
int main ( int, char ** )
{
    // Ask user for path to file.
    std::string path;
    std::cout << "filename:";
    std::getline(std::cin, path);
    // Open selected file.      
    std::ifstream file(path.c_str());
    if ( !file.is_open() )
    {
        std::cerr << "Failed to open '" << path << "'." << std::endl;
        return EXIT_FAILURE;
    }
    // Read lines (note: stores all of it in memory, might not be your best option).
    std::vector<std::string> lines;
    for ( std::string line; std::getline(file,line); )
    {
        lines.push_back(line);
    }
    // Print out (up to) last ten lines.
    for ( std::size_t i = std::min(lines.size(), std::size_t(10)); i < lines.size(); ++i )
    {
        std::cout << lines[i] << std::endl;
    }
}

避免将整个文件存储到内存中可能更明智,因此您可以这样重写最后2段:

// Read up to 10 lines, accumulating.
std::deque<std::string> lines;
for ( std::string line; lines.size() < 0 && getline(file,line); )
{
    lines.push_back(line);
}
// Read the rest of the file, adding one, dumping one.
for ( std::string line; getline(file,line); )
{
    lines.pop_front();
    lines.push_back(line);
}
// Print out whatever is left (up to 10 lines).
for ( std::size_t i = 0; i < lines.size(); ++i )
{
    std::cout << lines[i] << std::endl;
}

eof()函数并不像你和其他无数c++新手所想的那样。它不能预测下一次读取是否会工作。与其他语言一样,在c++中必须检查每个读操作的状态,而不是在读操作之前检查输入流的状态。所以规范的c++读行循环是:

while ( eingabe.getline(zeile,250) ) {
    // do something with zeile
}

另外,您应该读取std::string,并去掉250的值。

创建一个有10个槽的循环缓冲区,在读取文件行时,将它们放入该缓冲区。当你完成文件后,执行一个position++命令去到第一个元素并打印它们。如果文件少于10行,请注意空值。

    有一个大小为10的字符串数组。
  1. 读取第一行并存储到数组
  2. 继续读取,直到数组已满
  3. 一旦数组被填满,删除第一个条目,这样你就可以输入新的行重复步骤3和4,直到文件读取完成。

我研究了这里提出的方法,并在我的博客文章中进行了描述。有一个更好的解决方案,但你必须跳到最后并保存所有需要的行:

std::ifstream hndl(filename, std::ios::in | std::ios::ate);
// and use handler in function which iterate backward
void print_last_lines_using_circular_buffer(std::ifstream& stream, int lines)
{
    circular_buffer<std::string> buffer(lines);
    std::copy(std::istream_iterator<line>(stream),
              std::istream_iterator<line>(),
              std::back_inserter(buffer));
    std::copy(buffer.begin(), buffer.end(),
            std::ostream_iterator<std::string>(std::cout));
}