Updated June 8, 2023
Introduction to C++ ofstream
The standard library called iostream which is used to read from the standard input and write to the standard output by providing the methods cin and cout. Likewise there is another standard library in C++ called fstream to read the data from the file and to write the data into the file which provides the three data types namely ofstream, ifstream and fstream among which ofstream is a data type using which the output file stream is represented and files can be created and information can be written to the files and to make use of ofstream, the header file <fstream> must be included in the source file.
Syntax:
Given below is the syntax of C++ ofstream:
ofstream variable_name;
variable_name.open(file_name);
- variable_name is the name of the variable.
- file_name is the name of the file to be opened.
Working of C++ ofstream
- The standard library called iostream which is used to read from the standard input and write to the standard output by providing the methods cin and cout, likewise there is another standard library in C++ called fstream to read the data from the file and to write the data into the file.
- The standard library fstream provides three data types namely ofstream, ifstream and fstream.
- Whenever there is a need to represent the output file stream and to create a file and write information to the file, we make use of ofstream by including the header file <fstream> in the source file.
- Ofstream is derived from the class ostream class.
Examples of C++ ofstream
Given below are the examples mentioned:
Example #1
C++ program to demonstrate ofstream in a program to write the data to file and then read the contents from the file.
Code:
//The header file fstream is imported to enable us to use ofstream and ifstream in the program
#include <fstream>
//The header file iostream is imported to enable us to use cout and cin in the program
#include <iostream>
//The standard namespace std is used
using namespace std;
int main ()
{
// opening a file by name newfile using the ofstream data type to write contents into the file
ofstream file1("newfile.dat");
//By using the variable of ofstream data type, writing the contents into the file
file1 << "The contents written to the file are: Welcome to C++ " << endl;
//By using the variable of ofstream data type, closing the file that was opened to write the contents into the file
file1.close();
//defining a string variable
string read1;
//opening a file by name newfile using the ifstream data type to read contents from the file
ifstream file2("newfile.dat");
cout << "The contents in the file are : " << endl;
//using getline to read the data from the file line by line by making use of the string variable defined earlier
while (getline (file2, read1))
{
//The contents of the file read by using ifstream data type is printed as the output
cout << read1;
//By using the variable of ifstream data type, closing the file that was opened to read the contents from the file
file2.close();
}
}
Output:
Explanation:
- In the above program, the header file fstream is imported to enable us to use ofstream and ifstream in the program. Then another header file iostream is imported to enable us to use cout and cin in the program. Then the standard namespace std is used. Then the file by name newfile is opened using the ofstream data type to write contents into the file.
- Then by using the variable of ofstream data type, the contents is written into the file. Then by using the variable of ofstream data type, the file that was opened to write the contents into the file is closed.
- Then a string variable is defined. Then the file by name newfile is opened using the ifstream data type to read the contents from the file. Then by using the variable of ifstream data type, the contents is read from the file and display as the output. Then by using the variable of ifstream data type, the file that was opened to read the contents from the file is closed.
Example #2
C++ program to demonstrate ofstream in a program to write the data to file and then read the contents from the file.
Code:
//The header file fstream is imported to enable us to use ofstream and ifstream in the program
#include <fstream>
//The header file iostream is imported to enable us to use cout and cin in the program
#include <iostream>
//The standard namespace std is used
using namespace std;
int main ()
{
// opening a file by name newfile using the ofstream data type to write contents into the file
ofstream file1("filename.dat");
//By using the variable of ofstream data type, writing the contents into the file
file1 << "The contents written to the file are: Learning is fun" << endl;
//By using the variable of ofstream data type, closing the file that was opened to write the contents into the file
file1.close();
//defining a string variable
string read1;
//opening a file by name newfile using the ifstream data type to read contents from the file
ifstream file2("filename.dat");
cout << "The contents in the file are : " << endl;
//using getline to read the data from the file line by line by making use of the string variable defined earlier
while (getline (file2, read1))
{
//The contents of the file read by using ifstream data type is printed as the output
cout << read1;
//By using the variable of ifstream data type, closing the file that was opened to read the contents from the file
file2.close();
}
}
Output:
Explanation:
- In the above program, the header file fstream is imported to enable us to use ofstream and ifstream in the program. Then another header file iostream is imported to enable us to use cout and cin in the program. Then the standard namespace std is used. Then the file by name filename is opened using the ofstream data type to write contents into the file.
- Then by using the variable of ofstream data type, the contents is written into the file. Then by using the variable of ofstream data type, the file that was opened to write the contents into the file is closed. Then a string variable is defined. Then the file by name filename is opened using the ifstream data type to read the contents from the file.
- Then by using the variable of ifstream data type, the contents is read from the file and display as the output. Then by using the variable of ifstream data type, the file that was opened to read the contents from the file is closed.
Advantages
Given below are the advantages :
- << operator is supported by ofstream in C++.
- The contents of the file written using ofstream can be flushed automatically using the classes of fstream and the chances of corruption of file is less.
Recommended Articles
This is a guide to C++ ofstream. Here we discuss the introduction to C++ ofstream, working, advantages and respective programming examples. You may also have a look at the following articles to learn more –