DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   End Of File (ifstream) (http://www.daniweb.com/forums/thread123563.html)

Jennifer84 May 10th, 2008 3:20 pm
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 ?

        {
 
        }           
}

niek_e May 10th, 2008 3:22 pm
Re: End Of File (ifstream)
 
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.

Sky Diploma May 10th, 2008 3:28 pm
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?

Jennifer84 May 10th, 2008 3:41 pm
Re: End Of File (ifstream)
 
Thanks... you are right, that solved the problem :)
I will think about not using File too.

Quote:

Originally Posted by niek_e (Post 604352)
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.



All times are GMT -4. The time now is 5:21 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC