You are reading the article Working Of C++ Ofstream With Programming Examples updated in September 2023 on the website Benhvienthammyvienaau.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working Of C++ Ofstream With Programming Examples
Introduction to C++ ofstreamWeb development, programming languages, Software testing & others
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.
Ofstream is derived from the class ostream class.
Examples of C++ ofstreamGiven below are the examples mentioned:
Example #1C++ program to demonstrate ofstream in a program to write the data to file and then read the contents from the file.
Code:
using namespace std; int main () { ofstream file1("newfile.dat"); file1 << "The contents written to the file are: Welcome to C++ " << endl; file1.close(); string read1; ifstream file2("newfile.dat"); cout << "The contents in the file are : " << endl; while (getline (file2, read1)) { cout << read1; file2.close(); } }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 #2C++ program to demonstrate ofstream in a program to write the data to file and then read the contents from the file.
using namespace std; int main () { ofstream file1("filename.dat"); file1 << "The contents written to the file are: Learning is fun" << endl; file1.close(); string read1; ifstream file2("filename.dat"); cout << "The contents in the file are : " << endl; while (getline (file2, read1)) { cout << read1; 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
<< 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 ArticlesYou're reading Working Of C++ Ofstream With Programming Examples
Update the detailed information about Working Of C++ Ofstream With Programming Examples on the Benhvienthammyvienaau.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!