Detecting EOF 
Author Message
 Detecting EOF

Hi everyone,

I am very new to C++, let alone VC++ .NET.

Can someone please tell me how to detect the end of a file? Here's something
I've seen used elsewhere but I don't really understand it. This bit of code
is supposed to read from a file and input the lines into an array of
pointers, etc. Which part detects the end of file?

//--------------------------------------------------------------------------
-----------------------------
bool Menu::Load ()
{
 int i = 0;

    ifstream fin ("Menu.txt");
         if (!fin)
          {
               return false;
          }
         else
          {

               {
                   for (  i = 0 ; i < ITEMS && fin ; i++ )
                    {
                         if ( m_iNumberOfDishes >= ITEMS )return false;
                         m_apxDish_items[i] = new Dish(fin);    // uses Dish
method read
                         m_iNumberOfDishes++;                     // to
increase the number of dishes
                    }

               }
          }
 return true;

Quote:
}

//--------------------------------------------------------------------------
------------------------------

Any assistance would be immensely appreciated.

Cheers,
Dany.

--
This mail is a natural product.  The slight variations in spelling and
grammar enhance its individual character and beauty and in no way
are to be considered flaws or defects.



Tue, 13 Sep 2005 18:40:12 GMT  
 Detecting EOF

Quote:

> Which part detects the end of file?

>                    for (  i = 0 ; i < ITEMS && fin ; i++ )

This part. As you can see, the for loop continues as long as 'i < ITEMS &&
fin'. To evaluate fin as a boolean expression (true or false), it needs to
convert it to a number. The function that does that is basic_ios::operator
void*() (basic_ios is a base class of basic_ifstream, of which ifstream is a
special case for files with char data).

From the documentation of basic_ios::operator void*():
"The operator returns a null pointer only if fail."
See also:
ms-help://MS.VSCC/MS.MSDNVS/vcstdlib/html/vclrfbasiciosoperatorvoidasterisk.
htm

In other words, if the stream is bad (ie, cannot be read anymore), this
conversion yields NULL (0), which is equivalent to false in C++, so the loop
terminates.

Note that this method terminates the loop in any condition where the stream
can't be read anymore. This may indicate eof, but it may also indicate some
kind of error. To determine which of the two occurred, use the function
basic_ios::eof(), like this:

int i;
ifstream fin("Test.txt");
/* The for loop above is nice, but necessary only if you want to read the
stream till the end or up to a specific amount of data. If you just want to
read the stream until the end, use a while loop. Note that the if( !fin ) is
also not necessary, since the while loop won't execute if fin.operator void*
returns 0. The same would be true with the for loop by the way */
while( fin )
{
    // Read the stream.

Quote:
}

if( fin.eof() ) // successfully read the whole stream
    return true;
else // some error occurred
    return false;

--
Sven Groot



Wed, 14 Sep 2005 04:08:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. to detect eof with fseek ?

2. How to detect EOF?/filepointer problem.

3. how do you detect eof?

4. Question: Detecting EOF

5. detecting eof (file handles)

6. Prboblems detecting eof with feof() in C

7. Please help: can't detect an EOF while reading structures

8. EOF detect puzzlement

9. How can I detect the EOF..?

10. When Is EOF not EOF?

11. Find eof without using EOF

12. EOF or not EOF

 

 
Powered by phpBB® Forum Software