2011-04-26 134 views
4

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

雖然你可能已經知道這一點,UNIX [尾巴](http://en.wikipedia.org/wiki/Tail_(UNIX))命令究竟是幹什麼的你要。 – darioo 2011-04-26 18:45:45

+0

@darioo:這個問題聽起來像作業。 – 2011-04-26 18:57:33

+3

順便說一句,如果你尋找文件的末尾,然後向後尋找你的起點,而不是解析整個文件,你會發現它在更大的文件上快得多**。 – 2011-04-26 18:59:58

回答

0

做一個圓形緩衝器用10個槽和在讀取文件中的行,將它們放入該緩衝器中。當你完成thr文件時,做一個位置++去第一個元素並將它們全部打印出來。 如果文件少於10行,請留意空值。

0
  1. 有一個字符串與大小的數組10
  2. 閱讀的第一行,並存入數組
  3. 繼續閱讀,直到數組已滿
  4. 一旦數組已滿刪除第一項,以便您可以輸入新行 重複步驟3和4,直到文件讀完。
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的價值。

5

試試這個:

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

//一個知道如何讀取線使用運營商>>

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