Updated July 10, 2023
Introduction to File Handling in C++
The following article provides an outline of File Handling in C++. In programming, sometimes we need to read or write the data from or to the file, so C++ provides a standard library, fstream. We use the iostream standard library, iostream provides the cin method for reading from input and the cout method for writing to output. Similarly, to read from a file and write to a file, we can use C++ provided standard library fstream. The fstream provides different data types for different purposes.
The different data types of fstream library are as follows:
- ifstream: ifstream data type of fstream library acts as an input file stream used to read data from a file. To use this data type in the C++ program, we need to include a header file <ifstream>.
- ofstream: ofstream data type of fstream library acts as an output file stream used to write data to a file. To use this data type in the C++ program, we need to include a header file <ofstream>.
- fstream: fstream data type of fstream library generally acts as a file stream, which can be used for both ifstream and ofstream purposes. To use this data type in the C++ program, we need to include a header file <fstream>.
Reading from File in C++
As we read the data from the keyboard by using cin object and stream extraction operator (“>>”), in the same way, we can read data from a file into a program by using ifstream object and stream extraction operator (“>>”). Hence, the difference is cin which is an object of class istream instance. Of it, we will use the object of ifstream.
Writing to File in C++
Again as we write the data to the monitor by using the cout object and stream insertion operator (“<<”), the same way we can write data to a file from a program by using ofstream object and stream insertion operator (“<<”), so the difference is cout which is an object of class ostream instance of it we will use object of ofstream.
Examples of File Handling in C++
Given below are the examples of File Handling in C++:
Example #1
Example for reading and writing from or to a file.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char info[100], inp;
// open a file to write
ofstream of("data.txt");
cout << "Enter the data to store in the file:" << endl;
cout << "Enter your name: ";
cin.getline(info, 100);
// writing inputted informato to the file
of << info << endl;
cout << "Enter your phone number: ";
cin >> info;
cin.ignore();
// writing again to the file.
of << info << endl;
// close the file.
of.close();
cout<<"Do you want to read the information, if yes please enter 'y'"<<endl;
cin >> inp;
if(inp=='y'||inp=='Y')
{
// open for reading from file
ifstream ifs("data.txt");
cout << "Reading information from the file" << endl;
ifs >> info;
// writing information to the monitor
cout << info << endl;
// reading again the information from the file
ifs >> info;
// writing again information to the monitor
cout << info << endl;
// close the file.
ifs.close();
}
return 0;
}
Output:
In the above code, we use the getline() function to read a line from the keyboard and the ignore() function to ignore characters left by earlier read statements.
Example #2
Open a File with an open function.
The file can also be opened using the open() function. The open() function is a member of ifstream, ofstream, and fstream objects. An open() function for fstream or ofstream object can open a file for writing, and ifstream object is used to open a file for reading.
Syntax:
void open(const char *filename, ios::openmode )
Parameters:
1. *filename: This parameter represents the file’s name or location to be opened.
2. ios::openmode: This parameter represents the mode in which the file will be opened.
The possible values or possible modes by which a file can be open are five, which are given below:
- ios::in:Read mode: Open a file for reading.
- ios::out – write mode: Open a file for writing.
- ios::app – Append mode: Open a file to be appended output to the end of a file.
- ios::ate:Open a file and move the read or write pointer to the end.
- ios::trunk – truncate mode: The program will truncate the existing file contents before opening a file.
We can also open a file in more than one mode as well by just using ORing or | them together.
For example:
Open a file for both writing and reading as:
fstream iof;
iof.open("data.txt", ios::in | ios::out );
Example #3
Close a File in C++.
After reading and writing, we should close a file; if we open a file for writing mode or append, we must close a file; otherwise, we cannot see the updated data.
Syntax:
void close();
Example #4
Random access from File in C++.
There are two-pointer related to the istream and ostream that gets a pointer and a put pointer; the get pointer always points to the position of the next read operation, and the put pointer points to the position of next write operation. An istream and ostream provides some functions, which are tellg(return the position of the get pointer), tellp(return the position of the put pointer), seekg(“seek get pointer”, which skip the position of the get pointer), seekp(“seek put pointer”, which skip the position of the put pointer).
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char info[100], inp;
// open a file to write
fstream of;
of.open("data.txt", ios::in | ios::out);
cout<< "The position of the get pointer:"<< of.tellg()<<endl;
cout<< "The position of the put pointer:"<< of.tellp()<<endl;
of>>info;
cout<<"The original content of the file is:"<<endl;
cout<<info<<endl;
// change the position of the get pointer
of.seekg(5L,ios::beg);
cout<< "The position of the get pointer after seek:"<< of.tellg()<<endl;
of>>info;
cout<<"The data read from the file after seekp=5 with ios::beg: "<<info<<endl;
cout<< "The position of the get pointer now:"<< of.tellg()<<endl;
return 0;
}
Output:
Conclusion
C++ provides a standard library fstream for reading and writing a file. The file First, we need to open then, read or write a file, and then finally close the file, as we can see in the above example code.
Recommended Articles
We hope that this EDUCBA information on “File Handling in C++” was beneficial to you. You can view EDUCBA’s recommended articles for more information.