打印出文件的最后10行

问题描述 投票:4回答:6

我想要打印出文本文件的最后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;
    }
c++ file cout tail
6个回答
5
投票

试试这个:

#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"));
}

2
投票

基本上,您不是将文件内容保存到任何阵列。以下示例将为您提供一个良好的开端:

#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;
}

1
投票

eof()函数不会像你那样做,而且似乎有数百万其他C ++新手认为它。它不会预测下一次读取是否有效。在C ++中,与任何其他语言一样,您必须在读取之前检查每个读取操作的状态,而不是输入流的状态。所以规范的C ++读取行循环是:

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

此外,你应该阅读std::string,并摆脱250值。


0
投票

做一个带有10个插槽的循环缓冲区,同时读取文件行,将它们放入此缓冲区。当你完成thr文件时,做一个位置++来转到第一个元素并打印它们。如果文件少于10行,请注意空值。


0
投票
  1. 有一个大小为10的字符串数组。
  2. 读取第一行并存储到数组中
  3. 继续阅读直到阵列已满
  4. 阵列完全删除后,删除第一个条目,以便输入新行重复步骤3和4,直到文件读完为止。

0
投票

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

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));
}
© www.soinside.com 2019 - 2024. All rights reserved.