File Handling through C++
In C++, programs run in the computer’s RAM (Random Access
Memory), in which the data used by a program only exists while
the program is running. Once the program terminates, all the
data is automatically deleted. File handling allows us to
manipulate files in the secondary memory of the computer (like
the hard drive or SSD), using which we can store data
permanently and access it when required.
File Handling in C++
In C++, input and output are done in the form of a sequence of
bytes called streams. For example, cin and cout are the objects
associated with the standard input and output streams. These
streams are represented by different classes provided in the
<iostream> library.
Similarly, C++ also provides file stream classes to perform input
and output operations on files that are defined
inside <fstream> header file.
File Handling Operations
There are mainly three main steps in file handling:
Opening a File
Read/Write Operations
Closing a File
File Opening Modes
File opening mode indicate file is opened for reading,
writing, or appending. Below is the list of all file modes in
C++:
Mode Description
ios::in File open for reading. If file does not exists,
File open for writing: the internal stream buffer supports
ios::out output operations.
ios::bin Operations are performed in binary mode rather than
ary text.
ios::ate The output position starts at the end of the file.
Mode Description
All output operations happen at the end of the file,
ios::app appending to its existing contents.
ios::tru Any contents that existed in the file before it is open are
nc discarded.
For Example, if we want to open the file for reading, we
use the following opening mode:
fstream filein("file.txt", ios::in);
Similarly, if we want to open the file for writing, we use the
following:
fstream fileout("file.txt", ios::out);
These modes can also be combined using OR operator (|).
For example, you can open the file stream in both read and
write mode as shown:
fstream str("file.txt", ios::in | ios::out);
If the file opened in write mode does not exists, a new file is
created. But if the file opened in read mode doesn't exists,
then no new file is created, and an exception is thrown
Write Data to File
Once the file is opened in the write mode using
either fstream or ofstream, we can perform the write
operation in similar way as with cout using << operator.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
// Open a file
ofstream file("sample.txt");
// Write the string to the file
file << "Welcome to techziacademy.";
return 0;
}
Read Data from File
Once the file is opened in the read mode using either fstream or
ifstream, we can perform the write operation in similar way as
with cin using >> operator.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
// Open a file in read mode
ifstream file(“sample.txt");
string s;
// Read string from the file
file >> s;
cout << "Read String: " << s;
return 0;
}
Output
Read String: Welcome
Closing the File
Closing the file means closing the associated stream and free the
resources that we being used. It is important to close the file
after you are done with it, especially in the long running
programs to avoid memory leaks, data loss, etc.
In C++, the files are closed using the close() member function
that is present in all file streams.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Open a file in read mode
ifstream file("sample.txt");
string s;
// Read string from the file
getline(file, s);
cout << "Read String: " << s;
// Close the file
file.close();
return 0;
}
Output:
Read String: Welcome to techziacademy.