Advertisement



< Prev
Next >



C++ File and File Modes




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

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



How to open a file?


By using the object of any file stream class, we could call the open() function, using which we could provide location of the file stored on the disk and in return(if the file is found), the open() function opens the file in a specific mode to let you perform a specific file operation.

Let us take a look at a general syntax of open() function to open a file.

file-stream-object("filename", mode);



Advertisement




File Modes


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 Modes Description
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 tutorial 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. In the next tutorial, we are going to explain how to read an existing file on the disk and what if the file we are looking for does not exist? So, see you in the next one!




Please share this article -





< Prev
Next >
< Unformatted Output Function
Read a File >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement