954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

End Of File (ifstream)

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 ?

ifstream File("C:\\File1\\");
std::string Line;

while( getline(File, Line, ',') )
{

        if (         ?           )   // end of file ?

        {
   
        }            
}
Jennifer84
Posting Pro
564 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 
ifstream File("C:\\File1\\");
std::string Line;

while( getline(File, Line, ',') )
{
      // do stuff
} 
// 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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Well do you mean the end of the c++ program or the end of "file.txt" as you have mentioned in the program?

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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

ifstream File("C:\\File1\\");
std::string Line;

while( getline(File, Line, ',') )
{
      // do stuff
} 
// 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.

Jennifer84
Posting Pro
564 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You