944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2803
  • C++ RSS
Nov 3rd, 2009
0

Read Last ten lines from text

Expand Post »
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
*/
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xxunknown321 is offline Offline
24 posts
since Sep 2009
Nov 3rd, 2009
0
Re: Read Last ten lines from text
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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 3rd, 2009
0
Re: Read Last ten lines from text
is there a way to do it without a buffer? if so can you show me?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xxunknown321 is offline Offline
24 posts
since Sep 2009
Nov 3rd, 2009
0
Re: Read Last ten lines from text
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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 3rd, 2009
0
Re: Read Last ten lines from text
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xxunknown321 is offline Offline
24 posts
since Sep 2009
Nov 3rd, 2009
1
Re: Read Last ten lines from text
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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 3rd, 2009
0
Re: Read Last ten lines from text
thank you very much i will try my best to understand your code.
thank you again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xxunknown321 is offline Offline
24 posts
since Sep 2009
Nov 7th, 2011
-1
Re: Read Last ten lines from text
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.
C++ Syntax (Toggle Plain Text)
  1. 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.
C++ Syntax (Toggle Plain Text)
  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 usustarr; Nov 7th, 2011 at 8:25 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
usustarr is offline Offline
13 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do i search for a key word in last 100 lines of the file in C++?
Next Thread in C++ Forum Timeline: Multidimensional Arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC