Wednesday, February 24, 2010

EOF plays a major role sometimes..!

End of File Function


So, just how much data is in that file?  The exact contents of a file may not be precisely  known.  Usually the general format style of the file and the type of data contained within the file are known.  The amount of data stored in the file, however, is often unknown.  So, do we spend our time counting data in a text file by hand, or do we let the computer deal with the amount of data?  Of course, we let the computer do the counting.


  
C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise.


Rules for using end-of-file (eof( )):
1.  Always test for the end-of-file condition before processing data read from an input file stream.
     a.  use a priming input statement before starting the loop
     b.  repeat the input statement at the bottom of the loop body
2.  Use a
while loop for getting data from an input file stream.  A for loop is desirable only when you know the exact number of data items in the file, which we do not know.

No comments:

Post a Comment