| | |
Help reading data from a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 29
Reputation:
Solved Threads: 1
Hello everyone. I am trying to work through a programming challenge in my c++ book. I have gotten it to work, but i kind of cheated by looking at the file. I am going to past the entire code so i can be compiled, but the main problem i am having is with reading the last line of a file only.
the contents of file punchline.txt are:
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
"I can't work in the dark," he said.
I was trying to use the seekg member function. Am i thinking the right way about this? How do i (if i didnt look at the file) determine where to start printing the data if all i want is the last line, not the "garbage" in this test file. Any advice is much appreciated! Thank you
Jason
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; void dispJoke ( fstream& ); void dispPunchLine ( fstream& ); int main() { fstream joke ( "joke.txt", ios::in ); fstream punch ( "punchline.txt", ios::in ); if ( !joke ) { cout << "Erorr opening file, joke.txt"; return 0; } else if ( !punch ) { cout << "Erorr opening file, punchline.txt"; return 0; } dispJoke(joke); system("pause"); dispPunchLine(punch); joke.close(); punch.close(); return 0; } void dispJoke ( fstream& joke ) { char data[256]; joke.getline ( data, 256 ); while ( !joke.eof() ) { cout << data << endl; joke.getline ( data, 256 ); } } void dispPunchLine ( fstream& punch ) { char data[256]; punch.seekg(-35L, ios::end ); punch.getline ( data, 256, '.' ); while ( !punch.eof() ) { cout << data << endl; punch.getline ( data, 256, '.' ); } }
the contents of file punchline.txt are:
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
"I can't work in the dark," he said.
I was trying to use the seekg member function. Am i thinking the right way about this? How do i (if i didnt look at the file) determine where to start printing the data if all i want is the last line, not the "garbage" in this test file. Any advice is much appreciated! Thank you
Jason
The while statement is incorrect. The eof() function doesn't work the way you think it does.
After the above loop finishes data will contain the last line of the file.
C++ Syntax (Toggle Plain Text)
while( joke.getline(data, 256) ) { // blabla }
After the above loop finishes data will contain the last line of the file.
Last edited by Ancient Dragon; Nov 1st, 2008 at 12:27 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Reading data file in C (C)
- No documentation on reading in int from file (Python)
- read data file in C (C)
- reading data from file to vector (C++)
- Reading an input file as a class memeber function (C++)
- Perl/CGI (Reading Data) Part II (Computer Science)
- reading txt file into array (C++)
- Reading from external data file (C++)
Other Threads in the C++ Forum
- Previous Thread: zeroing out an array
- Next Thread: Printing a Queue...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






