943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4827
  • C++ RSS
May 10th, 2008
0

End Of File (ifstream)

Expand Post »
If I am reading a .txt file with ifstream, what is the syntax to recognice if I have reached the end of file. From what I have heard somewhere before, I think it has with eof to do ?

C++ Syntax (Toggle Plain Text)
  1. ifstream File("C:\\File1\\");
  2. std::string Line;
  3.  
  4. while( getline(File, Line, ',') )
  5. {
  6.  
  7. if ( ? ) // end of file ?
  8.  
  9. {
  10.  
  11. }
  12. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
May 10th, 2008
0

Re: End Of File (ifstream)

cpp Syntax (Toggle Plain Text)
  1. ifstream File("C:\\File1\\");
  2. std::string Line;
  3.  
  4. while( getline(File, Line, ',') )
  5. {
  6. // do stuff
  7. }
  8. // the end of the file is here

It's as simple as that

And I wouldn't use 'File' as a name because it looks to much like : FILE. Something like 'infile' would be better IMO.
Last edited by Nick Evan; May 10th, 2008 at 3:24 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 10th, 2008
0

Re: End Of File (ifstream)

Well do you mean the end of the c++ program or the end of "file.txt" as you have mentioned in the program?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 10th, 2008
0

Re: End Of File (ifstream)

Thanks... you are right, that solved the problem
I will think about not using File too.

Click to Expand / Collapse  Quote originally posted by niek_e ...
cpp Syntax (Toggle Plain Text)
  1. ifstream File("C:\\File1\\");
  2. std::string Line;
  3.  
  4. while( getline(File, Line, ',') )
  5. {
  6. // do stuff
  7. }
  8. // the end of the file is here

It's as simple as that

And I wouldn't use 'File' as a name because it looks to much like : FILE. Something like 'infile' would be better IMO.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008

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: importance of dllexport location in program
Next Thread in C++ Forum Timeline: very short code question plz help





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


Follow us on Twitter


© 2011 DaniWeb® LLC