OOPS - [C++ File Handling]

♠ Posted by Unknown in at 04:12

File Operation Mode


The ifstream and ofstream constructors and the function open() to create new files as well as to open the existing files. In both these methods, we used only one argument that was the filename.
These functions can take two arguments, the second one for specifying the file mode. The general form of the function open() with two arguments is:

            Stream_object.open(“filename”, mode);

The second argument mode specifies the purpose for which the file is opened.

1.       ios::our/ios::trunc
This mode opens a file into output (writing) mode. ios::out mode is default for open() function of ofstream class. Both are used to create new file if file is already exists it overwrites and create new blank file.

2.       ios::in
This mode opens a file into input(reading) mode. ios::in mode is default for open() function of ifstream class.

3.       ios::app
This mode opens file into output (writing) mode and append new record at the end of file if file already exists else creates new blank file.

4.       ios::ate
This mode opens a file to add data or to modify the existing data anywhere in the file if file already exists else creates new file. This mode puts file pointer to end of file on opening.

5.       ios::binary
This mode opens a file into binary mode not into text mode.   

6.       ios::nocreate
This mode is used with any mode to open an existing file rather then create new file. Using this mode open fails if the file does not exist instead of creating new file.

7.       ios::noreplace
This mode is usually used with output mode when opening of file it will already exists this mode avoid to replace an existing file.

2 comments:

File Handling in C++
A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of file. In c++ we can easily handle file for store data permanently.

File Handling in C++

File Handling concept is mainly used for store data permanently system or computer. Using file handling we can store our data in Secondary memory (Hard disk).

Post a Comment