What is File Handling?
File Handling stands for the manipulation of files storing relevant data using a programming language. This enables us to store data permanently, When we execute a program it gets loaded into main memory but in real-world programming manipulation of data sets occurs on a large scale so for it we don't use main memory in that case we store the data in drives and through stream connects it to programs and use only the data which is needed for the program.
Basics of File Handling in C++ :
Classes that help us in dealing with File Handling:-
fstream: This class is the parent class of ifstream and ofstream class, It is declared in the header file. It provides the input as well as output streams to operate on file. We can perform both read and write functions through this file.
ifstream: It is declared in fstream header file and is used to perform read functions on the file. It provides input stream to operate on the class.
ofsream: tIt is declared in fstream header file and is used to perform write functions on the file. It provides output stream to operate on the class.
How to open a file?
- Using Constructor to open file:-
Syntax:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// create an fstream object and open a file using the constructor
fstream file("example.txt", ios::out);
// write to the file
file << "Hello, world!";
// close the file
file.close();
return 0;
}
Explanation: In this code first we include the header file that is very important for working with files in C++ <fstream>
then in the main program we make an object of the class fstream
"file" then we open it in write mode using ios::out
after that we use << operator to write in the file.
Note that if the file does not exist, it will be created when you open it for output operations.
Opening modes: C++ provides various methods to open a file The opening mode's description and syntax are in the tabular form below.
Mode | Syntax | Description |
Read | ios::in | Opens file for reading purpose. |
Write | ios::out | Opens a file for writing purposes. |
Binary | ios::binary | All operations will be performed in binary mode. |
Truncate before Open | ios::trunc | If the file already exists, all content will be removed immediately. |
Append | ios::app | All provided data will be appended in the associated file. |
At End | ios::ate | It opens the file and moves the read/write control at the End of the File. The basic difference between the ios::app and this one is that the former will always start writing from the end, but we can seek any particular position with this one. |
How to close a file?
Closing a file in C++ file handling ensures data integrity, manages system resources effectively, allows access by other programs, enhances security, and improves file system performance. It is good practice to close files as soon as you are done using them to reap these advantages.
Syntax:
#include <fstream>
int main() {
std::ofstream outputFile("example.txt"); // Open file for writing
outputFile.close();
// Close the file
return 0;
}
How to write to a file?
We can write to a file using ofstream
class, and using operator <<
to write.
Syntax:
#include <iostream>
#include <fstream>
using namespace std;
int main() { // Create and open a text file
ofstream MyFile("filename.txt"); // Write to the file
MyFile << "Files can be tricky, but it is fun enough!";
// Close the file MyFile.close(); }
Explanation:
In this code we first use class ofstream
which is used to write into the file it is derived from the header file fstream.
and use <<
with obj which is MyFile in this case to write into the file.
How to read from the file?
To read from files we use ifstream class and >>
operator, ifsrtream
class is derived from the class fstream
which is header file.
Syntax:
// Create a text string, which is used to output the text file
string myText; // Read from the text file
ifstream MyReadFile("filename.txt"); // Use a while loop together with the
getline() //function to read the file line by line while
(getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText; }
// Close the file
MyReadFile.close();
What are file position pointers?
File postition pointers are used to navigate whithin the file they halp us in reading and writing in the file from specific positions. they provide flexibility and and control while working with files.
Some functions of file position pointers:-
tellp(): It returns the current position of put pointer while writing the data to file it is used with output stream.
tellg(): It returns the current position of get pointer while receiving the data from the file it is used with input stream.
seekg(): This function is used to move get pointer within the file get pointer is used to read data from the file .By using
seekg()
, you can set the position where the next read operation will occur.seekp(): This function is used to move put pointer within the file get pointer is used to write data in the file. By using
seekg()
, you can set the position where the next write operation will occur.
For instance, if you want to write data at the 20th byte of a file, you can use seekp(20)
to move the put pointer to that position. Subsequent write operations will start from that position in the file.
Hope this article helps you a lil bit.