C++ File Handling
As we know, at the time of execution, every program comes in main memory to execute.
Main memory is volatile and the data would be lost once the program is terminated.
If we need the same data again, we have to store the data in a file on the disk. A file
is sequential stream of bytes ending with an end-of-file marker.
Types of file supported by C++:
Difference between text file and binary file
- Text file is human readable because everything is stored in terms of text. In binary
file everything is written in terms of 0 and 1, therefore binary file is not human readable.
- A newline(\n) character is converted into the carriage return-linefeed combination before
being written to the disk. In binary file, these conversions will not take place.
- In text file, a special character, whose ASCII value is 26, is inserted after the
last character in the file to mark the end of file. There is no such special
character present in the binary mode files to mark the end of file.
- In text file, the text and characters are stored one character per byte. For example, the integer value
23718 will occupy 2 bytes in memory but it will occupy 5 bytes in text file. In binary file, the integer value
23718 will occupy 2 bytes in memory as well as in file.
The fstream.h Header File
C++'s standard library called fstream, defines the following classes
to support file handling.
- ofstream class : Provides methods for writing data into file. Such as, open(), put(), write(), seekp(), tellp(), close(), etc.
- ifstream class : Provides methods for reading data from file. Such as, open(),get(), read(), seekg(), tellg(), close(), etc.
- fstream class : Provides methods for both writing and reading data from file. The fstream class includes all the methods of ifstream and ofstream class.
Opening a file using open() member function
The open() function takes file-name argument. The purpose of opening the file i.e,
whether for reading or writing, depends on the object associated with open() function.
Example of opening file
fout.open("filename"); // Open file for writing
fin.open("filename"); // Open file for reading
// Using fstream, we can do both read and write, therefore,
// mode specifies the purpose for which the file is opened.
File Opening Modes
|ios::in||Open a text file for reading.|
|ios::out||Open a text file for writing. If it doesn't exist, it will be created.|
|ios::ate||Open a file and move the pointer at the end-of-file.|
|ios::app||Open a text file for appending. Data will be added at the end of the existing file. If file doesnt exist, it will be created.|
|ios::binary||Open file in a binary mode.|
|ios::nocreate||The file must already exist. If file doesn't exist, it will not create new file.|
|ios::trunc||If the file already exists, all the data will lost. |
Reading and Writing into File
We can read data from file and write data to file in four ways.
- Reading or writing characters using get() and put() member functions.
- Reading or writing formatted I/O using insertion operator ( << ) and extraction operator ( >> ).
- Reading or writing object using read() and write() member functions.