| | |
Read Last ten lines from text
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 8
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 20 Days Ago
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; 20 Days Ago 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 20 Days Ago
"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 20 Days Ago
•
•
•
•
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; 20 Days Ago 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
| Thread Tools | Search this Thread |
.htaccess .net arithmetic array assembly bmp box c# c++ calling change code compression console csv cursor definedlines delete delete-line directory download edit enter ereader error experience file fstream functions getline handling header htaccess i/o iframe ifstream image input itunes java javascript kernel keyboard line lines linked linux list lists match mouse mp4 number numeric open output parameter parsing passing php pointer position process program python read reading regex remote split store stream string strings text text-file textbox trick trim tuple update upload url user validate validation variables vbnet web write







