C++ file opening modes

C++ file opening 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 both these methods , we used only 1(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 also therefore they use the default values in the absense of 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

the file mode parameter can take one or more of such constant defined in the class ios in the below table list the file mode parameters and their meanings

Table file mode parameters

Parameter meaning
ios::app Append to end of life
ios::ate Go to end of file on opening
ios::binary Binary file
ios::in open file for reading only
ios::nocreate can’t fails if the file does not exist
ios::noreplace open files if the file already exists
ios:out open file for writing only
ios:trunc Delete the contents of the file if it exists
  • 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