When it comes to file input and output (I/O) in C++, the std::fstream object provides a powerful and flexible way to read from and write to files. Whether you need to read data from a file, write data to a file, or perform both operations simultaneously, the std::fstream object is a great choice.
Reading from a File
To read data from a file using std::fstream, you will need to perform the following steps:
- Include the necessary header file:
#include <fstream> - Declare an
std::fstreamobject and open the file in read mode:std::fstream file; file.open("filename.txt", std::ios::in);Note: Replace “filename.txt” with the actual name and path of your file.
- Check if the file successfully opened:
if (file.is_open()) { // File opened successfully, proceed with reading // Read data using the file object } else { // Failed to open the file } - Read data from the file:
std::string line; while (std::getline(file, line)) { // Process each line of data } - Close the file after reading is complete:
file.close();
Writing to a File
To write data to a file using std::fstream, you will need to perform the following steps:
- Include the necessary header file:
#include <fstream> - Declare an
std::fstreamobject and open the file in write mode:std::fstream file; file.open("filename.txt", std::ios::out);Note: Replace “filename.txt” with the actual name and path of your file.
- Check if the file successfully opened:
if (file.is_open()) { // File opened successfully, proceed with writing // Write data using the file object } else { // Failed to open the file } - Write data to the file:
file << "Hello, file!" << std::endl; file << "Data to be written." << std::endl; - Close the file after writing is complete:
file.close();
Simultaneous Reading and Writing
The std::fstream object also allows simultaneous reading and writing operations on a file. To achieve this, you need to open the file in both input and output modes:
std::fstream file;
file.open("filename.txt", std::ios::in | std::ios::out);
From this point, you can use the file object to both read from and write to the file.
#C++ #FileIO