Help reading data from a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Help reading data from a file

 
0
  #1
Nov 1st, 2008
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.

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. void dispJoke ( fstream& );
  7. void dispPunchLine ( fstream& );
  8.  
  9. int main()
  10. {
  11. fstream joke ( "joke.txt", ios::in );
  12. fstream punch ( "punchline.txt", ios::in );
  13.  
  14. if ( !joke )
  15. {
  16. cout << "Erorr opening file, joke.txt";
  17. return 0;
  18. }
  19. else if ( !punch )
  20. {
  21. cout << "Erorr opening file, punchline.txt";
  22. return 0;
  23. }
  24.  
  25. dispJoke(joke);
  26. system("pause");
  27. dispPunchLine(punch);
  28.  
  29. joke.close();
  30. punch.close();
  31.  
  32. return 0;
  33. }
  34. void dispJoke ( fstream& joke )
  35. {
  36. char data[256];
  37. joke.getline ( data, 256 );
  38. while ( !joke.eof() )
  39. {
  40. cout << data << endl;
  41. joke.getline ( data, 256 );
  42. }
  43. }
  44. void dispPunchLine ( fstream& punch )
  45. {
  46. char data[256];
  47.  
  48. punch.seekg(-35L, ios::end );
  49. punch.getline ( data, 256, '.' );
  50. while ( !punch.eof() )
  51. {
  52. cout << data << endl;
  53. punch.getline ( data, 256, '.' );
  54. }
  55. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help reading data from a file

 
0
  #2
Nov 1st, 2008
The while statement is incorrect. The eof() function doesn't work the way you think it does.
  1. while( joke.getline(data, 256) )
  2. {
  3. // blabla
  4. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: Help reading data from a file

 
0
  #3
Nov 1st, 2008
Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help reading data from a file

 
0
  #4
Nov 1st, 2008
AD is correct. Read This
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC