26 Nov 2019

  • November 26, 2019
  • Amitraj
C++ File and File Modes

C++ language allows us to perform important Disk input/output operations such as -

-> Creating a new file on the disk.
-> Reading the file stored on the disk.
-> Writing data to the file stored on the disk.
-> Appending new data to the end of the file stored on the disk.
-> Modifying the content of the file stored on the disk.


To perform any file operations, C++ provides us a few file stream classes, such as -

-> ifstream, to perform the file input operations.
-> ofstream, to perform the file output operations.

-> fstream, to perform any file input and output operations.


-> In C++, for every file operation, exists a specific file mode. These file modes allow us to create, read, write, append or modify a file. The file modes are defined in the class ios. Let's see all these different modes in which we could open a file on disk.



File ModesDescription
ios::in
Searches for the file and opens it in the read mode only(if the file is found).
ios::out
Searches for the file and opens it in the write mode. If the file is found, its content is overwritten. If the file is not found, a new file is created. Allows you to write to the file.
ios::app
Searches for the file and opens it in the append mode i.e. this mode allows you to append new data to the end of a file. If the file is not found, a new file is created.
"ios::binary"
Searches for the file and opens the file(if the file is found) in a binary mode to perform binary input/output file operations.
ios::ate
Searches for the file, opens it and positions the pointer at the end of the file. This mode when used with ios::binary, ios::in and ios::out modes, allows you to modify the content of a file.
"ios::trunc"
Searches for the file and opens it to truncate or deletes all of its content(if the file is found.
"ios::nocreate"
Searches for the file and if the file is not found, a new file will not be created.



-> This concept of file modes has introduced you to some important disk input/output operations that we can perform on a file and modes in which we can open a file on disk. 


Translate

Popular Posts