C++ EOF End of file
Detection of the end of file condition is necessary for preventing any further access and read data from the file. This was use in previous programs using the statement
An ifstream object, such as fun, returns a value of 0 if any error occurs in the file
operation including the end of file condition. Thus the while loop terminates when fin return a value of zero on reaching the end of file condition. Note: this loop may terminate due to other failures as well
There is another approach to detect the end of the file condition.
Note that we have used the following statement in previous programs
eof() is a member function of ios class. it returns a non zero value of the end of file (EOF) condition is encountered, and a zero(0), otherwise therefore the above statement terminates the program on reaching the(EOF) or end of the file.
MORE ABOUT OPEN(): FILE MODES
we have used ifstream and ofstream constructor and the function open() to create new files as well as to open the existing files. Remember in both these methods , we used only one argument that was
the filename . whenever these functions can take two arguments the second one for specifying the file mode. let us take The general form of the function open() with two arguments is
stream-object.open(“Filename”,mode);
The second argument mode (called file mode parameter) specifies the purpose for which the file is opened how did we then open the files without providing the second argument in the
previous example
The prototype of these class member functions contains default values for the second argument and therefore they use the default values in the absense of the actual values. The default values are as
follows
ios::in for ifstream functions meaning open for reading only
ios::out for osftream functions meaning open for writing only