File IO in C++ (text and binary files)
This C++ programming language tutorial will be in two parts. The first (this one) will cover the theory behind IO and in the second tutorial we will look at some examples.
Input-Output
Input – Output is a process of transfer of data from one computer device to another or from one part of the computer to another.
There are three categories of Input-Output:
- Standard IO
- Memory IO
- Network IO
Standard IO is used frequently for which C++ provides cin, cout, cerr and clog streams. IO in C++ is done through stream classes, which are having the following inheritance hierarchy:
We can use the ifstream, ofstream and fstream classes to perform file IO. (cin is an object of class istream and cout is an object of class ostream.)
File IO means transfer of data from secondary memory (hard disk) to main memory or vice-versa. A schematic showing the flow of data and classes involved is as follows:
Note: The arrows indicate the flow of data.
Text and binary files
The C++ language supports two types of files:
- Text files
- Binary files
The basic difference between text files and binary files is that in text files various character translations are performed such as “\r+\f” is converted into “\n”, whereas in binary files no such translations are performed.
By default, C++ opens the files in text mode.
In the tables below we will see the various steps and operations that can (or must) be performed to use files in C++:
1)Creating or opening a
file
|
|
Text Files
ofstream out (“myfile.txt”); or ofstream out; out.open(“myfile.txt”); |
Binary Files
ofstream out (“myfile.txt”,ios::binary); or ofstream out; out.open(“myfile.txt”, ios::binary); |
|
|
Text Files
ofstream out(“myfile.txt”,ios::app); or ofstream out; out.open(“myfile.txt”, ios::app); |
Binary Files
ofstream out or ofstream out; out.open(“myfile.txt”, ios::app | ios::binary); |
|
|
Text Files
ifstream in (“myfile.txt”); or ifstream in ; in.open(“myfile.txt”); |
Binary Files
ifstream in (“myfile.txt”, ios::binary); or ifstream in ; in.open(“myfile.txt”, ios::binary); |
2) Closing Files (after reading or writing)
ofstream object “out” |
Ifstream object “in” |
out.close(); | in.close(); |
3) Reading / Writing Data to and from files
Data | Functions for reading file |
Function for writing into file |
char | get(); | put(); |
1 word |
>> (extraction operator) |
<< (insertion operator) |
>=1 word |
getline(); | << (insertion operator) |
Objects | read() | write() |
Binary data |
Same as above |
Same as above |
4) Functions that can be used to perform special tasks
Operation | function | Description |
Checking end of file. |
eof() | Used to check eof during the reading of file |
Check if an operation fails. |
bad() |
Returns true |
Check if an operation fails. |
Fail() |
Returns true |
Checking for opened file. |
is_open(); | Checks if the file is opened or not, returns true if the file is opened else false |
Number of bytes already read. |
gcount() | Returns count of the bytes read from the file |
Ignoring characters during file read. |
ignore() | Ignores n bytes from the file. (get pointer is positioned after n character) |
Checking next character. |
peek() | Checks the next available character, will not increase the get pointer to next character. |
Random access (only for binary files). |
seekg()
seekp() tellg() tellp() |
In case of binary files random access is performed using these functions. They either give or set the position of get and put pointers on the particular location |
That is all for this C++ tutorial.
Good Work..
Thank you for making that page.
Thanks….this content will be very much help full for us.
Very good work, the answer is to the point. It will help students to learn, understand and write better.