C++ EOF End of file

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

while(fin)

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

if(fin1.eof()!=0)
{
exit(1);
}

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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise