Updated April 3, 2023
Introduction to C++ file operation
The following article provides an outline for C++ file operation. C++ provides different options with respect to file, which means the user can perform different operations on the file. Mainly, a file is used to store data on a device permanently. The file handling provides a facility to store the output of the program in a file and perform different operations on it. In file operation, we use to stream it used to represent the source and destination of character and which operation is to be performed either read or write operations. The file handling provides different mechanisms to perform operations such as fstream, ofstream, and ifstream. These classes are developed to manage files. Therefore we must include these classes in every file handling program.
Syntax
Here are the following syntax mention below
1. File Opening
void open (File name, ios:: open mode of file);
Explanation
In the above syntax, void open is used to open the file, where the file name represents the specified file name, and the mode of the file represents the mode in which we want to open the file.
2. File Writing
void open (File name, ios:: out);
Explanation
In the above syntax, void open is used to open the file, the file name is the specified file name that we want to open, and file mode out represents write mode.
3. Close File
File name. close();
Explanation
In the above syntax, file name means the specified file name that we want to close and the close() function is used to close the file.
File operation in C++
C++ mainly works with the following classes as follows
- ofstream: The ofstream class represents the output file stream, and it is used to create the file to write and data to file.
- ifstream: This class represents the input file stream, and it is used to read data from files.
- fstream: This class represents both output and input file stream that means read and write file operations.
Let’s see different file operations as follows.
1. File Opening
In this operation, suppose the user needs to open a file at that time we use this operation. The C++ provides different modes to users for opening a file. Let’s see different modes of file operation as follows.
Modes | Explanation |
in | This mode is used to open files in reading mode, and this is by default mode for ifstream. |
out | This mode is used to open the file in write mode, and this is by default mode for ofstream |
binary | This mode is used to open the file in binary mode. |
app | The app means Append mode of the file, and it is used to add all output of the file to the end. |
ate | This mode is used to open the file and pass the control at the end of the file. |
trunk | This mode is used to remove the data from the existing file. |
Example: Create and open the file by using the open() function
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_object;
new_object.open("sample",ios::out);
if(new_object)
{
cout<<"file not created ";
}
else
{
cout<<"New file created";
}
return 0;
}
Explanation:
In the above example, first, we declared a header file iostream and fstream; in this code, we create a new_object object for the fstream class then access new_object with open() function, then we give the new file name as a sample without file mode which allows us to write operation to that file. Here we use an if statement. If the file already exists, it shows a file not created, and if the file does not exist, then it shows a new file created. Finally, illustrate the end result of the above declaration by using the use of the following snapshot.
2. Read and write file
In this operation, we read data from a file by using the ifstream or fstream class, and the basic difference between a simple program and a file handling program is only using ifstream or fstream instead of cin. Similarly, we can say in write file mode we use ofstream or fstream class to write data, and the difference is only used ofstream or fstream instead of cout. So let’s see an example for a better understanding.
Example:
#include <fstream>
#include <iostream>
using namespace std;
int main () {
char info[50];
ofstream ofile;
ofile.open("sample1.dat"); //open file
cout << "Write emp information to file" << endl;
cout << "Enter emp name: "; // write data into file
cin.getline(info, 50);
ofile << info << endl;
cout << "Enter emp contact no: "; // write data into file
cin >> info;
cin.ignore();
ofile << info << endl;
ofile.close(); // close the file
ifstream ifile;
ifile.open("sample1.dat"); // open file in read mode
cout << "Reading information from the file" << endl;
ifile >> info;
cout << info << endl; // read data and display
ifile >> info;
cout << info << endl; // read data and display
ifile.close(); // close the file
return 0;
}
Explanation:
In the above example, we perform two file operations, such as read and write operations. In the first part of the program, we open files and perform write operations. In that file, here we write the information like emp name and emp contact number. In the second part of the program, we read information from a file. Illustrate the end result of the above declaration by using the use of the following snapshot.
3. Close file
In this operation, we simply close the file using the close() function. Let’s see an example for a better understanding.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream newfile;
newfile.open ("emp.txt");
if(newfile.is_open())
{
cout<<"emp file is open"<<endl;
newfile.close();
cout<<"emp file close "<<endl;
}
else{
cout<<"Error in file "<<endl;
}
return 0;
}
Explanation:
In the above example, we first open an emp file by using the open() function, then we check if the file is open or not. Finally, illustrate the end result of the above declaration by using the use of the following snapshot.
Conclusion
We hope from this article you have understood the C++ file operations. From the above article, we have learned the basic different syntax of the file operation. We have also learned how we can implement them in C++ with different examples of each type. For example, from this article, we have learned how we can handle fstream, ifstream, and ofstream in c++.
Recommended Articles
This is a guide to C++ File Operation. Here we discuss how to implement them in C++ along with the different examples of each type. You may also have a look at the following articles to learn more –