| | |
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:
Solved Threads: 0
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
*/
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
*/
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.
C++ Syntax (Toggle Plain Text)
#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; }
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
0
#4 Nov 3rd, 2009
"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
1
#6 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.
C++ Syntax (Toggle Plain Text)
#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; }
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
![]() |
Similar Threads
- Counting lines in a text file and further operations (C++)
- Common lines in text files (C#)
- easist way to find out # of lines in a text? (C++)
- read-delete lines from a text file (PHP)
- Read a specific line from a text file - how to ? (Java)
- # of lines in a text file (Java)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
Other Threads in the C++ Forum
- Previous Thread: Need help with FOR repitition with Switch
- Next Thread: Text - Based RPG V2
Views: 668 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for file, input, output, reading, text-file
.net append applicatio arithmetic array arrays assembly associations binary bits box c# c++ calling change code compress compression database definedlines delete delete-line deserialized directory display download edit enter ereader error experience file file-dialog filechooser files fstream functions getline handling header htaccess i/o input itunes java javascript kernel keyboard line lines linked linux list loop looping match mouse mp4 mysql number numeric ofstream open output parameter parsing passing permissions php pointer position print process python read reading regex remote saving search store storing stream string strings text text-file textbox trick trim tuple update upload user validate validation vbnet view web write







