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
|