Introduction to C++ fstream
In C++ the concept of the fstream is used for the reading and writing on the file system. In very simple and technical words we can say it has ability to do dual work which means it has ofstream and ifstream. So in case if the file is not there on which we are going to write some contents then the fstream has capability to write the contents on the file and at the same time it also allows us to open the file and display the contents of the files. We should use this if we know that we are going to create, read and write contents on the file.
Syntax:
Below is a simple syntax for the fstream in the c++. In the below example first we are getting or creating a file, we can give any name to file which we are creating here. Second we are writing some contents to the file. In the same way we can read the file content with help of the getline function in while loop.
ofstream creatMyFile(AnyFileName);
creatMyFile << Any text as contents;
How fstream work in C++?
We have already package like ifstream and ofstream but they can either read and write the file, but what we do in case if we want to perform read and write the file? So for that case we have fstream c++ package.
We can create a file if file does not exists like.
- Here first we can create a file instance with code like “ofstream of”, here of will be used as the instance.
- Next we can pass any name of file which we want to create like “open(any filename);”.
- Finally, we can write the contents on the file like cout << “any contents and text data” << endl;
- If needed, then we can also read the contents of the file with the help of the functions of getline to read data line by line.
Examples of C++ fstream
Given below we have three important examples of the fstream of the c++. In the examples we are showing how we are creating an empty file and writing some contents on the file and then again reading the same file with printing all the contents of the file.
Example #1
Code:
//Importing the package iostream
#include <iostream>
//Importing the package fstream
#include <fstream>
//Importing the string package for string related works
#include <string>
using namespace std;
int main () {
string ln;
//Creating a file with name test.txt ,if not exists
ifstream testFile ("test.txt");
//Checking the file opening condition
if (testFile.is_open())
{
//Running a while loop and fetch all the contents line by line
while ( getline (testFile,ln) )
{
//Printing the output of the file contents
cout << ln << '\n';
}
//Here we are closing the opened file
testFile.close();
}
else cout << "File is not there on the given path";
return 0;
}
Output:
Example #2
Code:
//Importing the package iostream
#include <iostream>
//Importing the package fstream
#include <fstream>
//Importing the package string for string related work
#include <string>
using namespace std;
int main () {
// This code will create a file with name test.txt
ofstream creatMyFile("test.txt");
// Here we are writing contents to the file
creatMyFile << "Hello, C++ is a powerful language";
// Once done with the writing closing the file
creatMyFile.close();
// Here we have created a text file to read the contents and show as the output on the screen
string myText;
// Here we are going to read the file
ifstream readMyFile("test.txt");
// Here we are running the loop and using the function getline and reading each lines of the file.
while (getline (readMyFile, myText)) {
// Output the contents from the file
cout << myText;
}
// Here we are closing the opened file
readMyFile.close();
}
Output:
Example #3
Code:
//Importing the package fstream
#include <fstream>
//Importing the package iostream
#include <iostream>
using namespace std;
int main () {
char subject[200];
// Here we are opening the file in the write mode for operations
ofstream of;
//Open the file and create the file if not exists
of.open("test.txt");
//Writing the the data to the file which we have created
cout << "Here we are writing this to the file" << endl;
cout << "My name is Ranjan Kumar pandey";
cin.getline(subject, 200);
// write inputted data into the file.
of << subject << endl;
cout << "Enter your age: ";
cin >> subject;
cin.ignore();
// Here we are again writing some input data to file
of << subject << endl;
// close the opened file.
of.close();
// Here we are opening the file in read purpose
ifstream inf;
inf.open("test.txt");
cout << "is the file contents" << endl;
inf >> subject;
// Writing some data to it
cout << subject << endl;
// Here we are again going to read the file data and also we are displaying the data
inf >> subject;
cout << subject << endl;
// Here we are performing closing of the file which we have opened
inf.close();
return 0;
}
Output:
Advantages of C++ fstream
Given below are the advantages mentioned :
It has the ability to perform dual work like it can create a file and at the same time it allows you to write the contents on the file.
- One of the most important things about it is, it allows us to use the concept of internalization and localization.
- It gives us a complete object oriented approach. Because of which we can reuse the features many times.
- Because it has a feature where if the file does not exist instead of throwing an error it will create the file for us.
Conclusion
From this tutorial we saw the basic concept of the fstream and we saw about the syntax of it. We also focused on the working and some of the important advantages of using the concept of the fstream. We saw some of the important examples also.
Recommended Articles
This is a guide to C++ fstream. Here we discuss the introduction to C++ fstream, how fstream work in with programming examples and advantages. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses