How do i get my program to read the last ten lines of the ".txt" file?
Can you please dumb it down as much as possible.
Here is what i have so far:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  string line;
  ifstream myfile ("test.txt");

  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

/*
An output example:
25: this is line 25
26: this is line 26
27: this is line 27
28: this is line 28
29: this is line 29
30: this is line 30
31: this is line 31
32: this is line 32
33: this is line 33
34: this is line 34
assuming the .txt file only had 34 lines
*/

Recommended Answers

All 7 Replies

I'd just read all lines of the file into a circular buffer with 10 strings, each containing a line. When you get to the end of the file, you will have the last 10 lines stored.

#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
using std::getline;

int main()
{
   ifstream myfile("test.txt");
   string line, buffer[10];
   const size_t size = sizeof buffer / sizeof *buffer;
   size_t i = 0;
   /*
    * Read all lines of file, putting them into
    * a circular buffer of strings.
    */
   while ( getline(myfile, line) )
   {
      buffer[i] = line;
      if ( ++i >= size )
      {
         i = 0;
      }
   }
   /*
    * Print the lines.
    */
   for ( size_t j = 0; j < size; ++j )
   {
      cout << buffer[i] << "\n";
      if ( ++i >= size )
      {
         i = 0;
      }
   }
   return 0;
}

is there a way to do it without a buffer? if so can you show me?

is there a way to do it without a buffer? if so can you show me?

I suppose you could read the file to count the lines and then back up and only print the last 10.

Why the extra condition now?

Why don't you give the coding a shot first?

well i pretty dum when it comes to c++ (a literal newbie)
i asked for it t o be simplified if possible to make it a bit easier to understand.

well i pretty dum when it comes to c++ (a literal newbie)
i asked for it t o be simplified if possible to make it a bit easier to understand.

Well, choose your poison: multiple passes of the same file, or a single read with a buffer. Both are relatively simple, I'd recommend just examining what's there. Experiment a bit. Learning to code seems to be what you're after, so spend some time learning.

#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
using std::getline;

size_t count_lines(const char *filename)
{
   ifstream myfile("test.txt");
   string line;
   size_t count = 0;
   while ( getline(myfile, line) )
   {
      ++count;
   }
   return count;
}

int main()
{
   const char filename[] = "test.txt";
   size_t i, count = count_lines(filename);
   ifstream myfile(filename);
   string line;
   for ( i = 0; i < count - 10; ++i )
   {
      getline(myfile, line); /* read and discard: skip line */
   }
   while ( getline(myfile, line) )
   {
      cout << line << "\n";
   }
   return 0;
}

Simpler?

commented: thnx so much +1

thank you very much i will try my best to understand your code.
thank you again

How would I search for a phrase in last 100 lines. I did use your code to read last 100 lines, but now I need to search for "key word is" in last 100 lines (phrase I am searching is a char string). It is declared as follows.

char searchForCode[STRING_MAX];

Can You help?

Well, choose your poison: multiple passes of the same file, or a single read with a buffer. Both are relatively simple, I'd recommend just examining what's there. Experiment a bit. Learning to code seems to be what you're after, so spend some time learning.

#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
using std::getline;

size_t count_lines(const char *filename)
{
   ifstream myfile("test.txt");
   string line;
   size_t count = 0;
   while ( getline(myfile, line) )
   {
      ++count;
   }
   return count;
}

int main()
{
   const char filename[] = "test.txt";
   size_t i, count = count_lines(filename);
   ifstream myfile(filename);
   string line;
   for ( i = 0; i < count - 10; ++i )
   {
      getline(myfile, line); /* read and discard: skip line */
   }
   while ( getline(myfile, line) )
   {
      cout << line << "\n";
   }
   return 0;
}

Simpler?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.