Read Last ten lines from text

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 13
Reputation: xxunknown321 is an unknown quantity at this point 
Solved Threads: 0
xxunknown321 xxunknown321 is offline Offline
Newbie Poster

Read Last ten lines from text

 
0
  #1
Nov 3rd, 2009
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
*/
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 256
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #2
Nov 3rd, 2009
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.
  1. #include <iostream>
  2. using std::cout;
  3. #include <fstream>
  4. using std::ifstream;
  5. #include <string>
  6. using std::string;
  7. using std::getline;
  8.  
  9. int main()
  10. {
  11. ifstream myfile("test.txt");
  12. string line, buffer[10];
  13. const size_t size = sizeof buffer / sizeof *buffer;
  14. size_t i = 0;
  15. /*
  16.   * Read all lines of file, putting them into
  17.   * a circular buffer of strings.
  18.   */
  19. while ( getline(myfile, line) )
  20. {
  21. buffer[i] = line;
  22. if ( ++i >= size )
  23. {
  24. i = 0;
  25. }
  26. }
  27. /*
  28.   * Print the lines.
  29.   */
  30. for ( size_t j = 0; j < size; ++j )
  31. {
  32. cout << buffer[i] << "\n";
  33. if ( ++i >= size )
  34. {
  35. i = 0;
  36. }
  37. }
  38. return 0;
  39. }
Last edited by Dave Sinkula; Nov 3rd, 2009 at 1:20 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: xxunknown321 is an unknown quantity at this point 
Solved Threads: 0
xxunknown321 xxunknown321 is offline Offline
Newbie Poster
 
0
  #3
Nov 3rd, 2009
is there a way to do it without a buffer? if so can you show me?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 256
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
Nov 3rd, 2009
Originally Posted by xxunknown321 View Post
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: xxunknown321 is an unknown quantity at this point 
Solved Threads: 0
xxunknown321 xxunknown321 is offline Offline
Newbie Poster
 
0
  #5
Nov 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 256
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
1
  #6
Nov 3rd, 2009
Originally Posted by xxunknown321 View Post
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.
  1. #include <iostream>
  2. using std::cout;
  3. #include <fstream>
  4. using std::ifstream;
  5. #include <string>
  6. using std::string;
  7. using std::getline;
  8.  
  9. size_t count_lines(const char *filename)
  10. {
  11. ifstream myfile("test.txt");
  12. string line;
  13. size_t count = 0;
  14. while ( getline(myfile, line) )
  15. {
  16. ++count;
  17. }
  18. return count;
  19. }
  20.  
  21. int main()
  22. {
  23. const char filename[] = "test.txt";
  24. size_t i, count = count_lines(filename);
  25. ifstream myfile(filename);
  26. string line;
  27. for ( i = 0; i < count - 10; ++i )
  28. {
  29. getline(myfile, line); /* read and discard: skip line */
  30. }
  31. while ( getline(myfile, line) )
  32. {
  33. cout << line << "\n";
  34. }
  35. return 0;
  36. }
Simpler?
Last edited by Dave Sinkula; Nov 3rd, 2009 at 2:20 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: xxunknown321 is an unknown quantity at this point 
Solved Threads: 0
xxunknown321 xxunknown321 is offline Offline
Newbie Poster
 
0
  #7
Nov 3rd, 2009
thank you very much i will try my best to understand your code.
thank you again
Reply With Quote Quick reply to this message  
Reply

Tags
file, input, output, reading, text-file

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 668 | Replies: 6
Thread Tools Search this Thread



Tag cloud for file, input, output, reading, text-file
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC