Skip to content

File I/O

File input/output (I/O) is an essential part of programming. It allows you to read data from files and write data to files. In C++, file I/O is done using streams. A stream is a sequence of bytes that represent a flow of data. There are two types of streams in C++:

  • Input Stream: Used to read data from a file.
  • Output Stream: Used to write data to a file.

In C++, file I/O is done using the fstream class, which is defined in the <fstream> header file. The fstream class provides member functions for reading and writing data to files.

Opening a File

Before you can read from or write to a file, you need to open it. To open a file, you need to create an object of the fstream class and call the open() member function. The open() member function takes two arguments:

  • The name of the file you want to open.
  • The mode in which you want to open the file (ios::in for input, ios::out for output, ios::app for append, etc.).

Here is an example of opening a file for writing:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file;
file.open("data.txt", ios::out); // The ios::out mode opens the file for writing
if (!file) {
cout << "Error opening file!" << endl;
return 1;
}
file << "Hello, World!" << endl;
file.close();
return 0;
}

In this example, we create an fstream object called file and open the file data.txt for writing using the ios::out mode. If the file cannot be opened, an error message is displayed. We then write the string "Hello, World!" to the file and close it.

Reading from a File

To read data from a file, you can use the >> operator with an fstream object. Here is an example of reading from a file:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file;
file.open("data.txt", ios::in); // The ios::in mode opens the file for reading
if (!file) {
cout << "Error opening file!" << endl;
return 1;
}
string data;
file >> data;
cout << "Data read from file: " << data << endl;
file.close();
return 0;
}

In this example, we open the file data.txt for reading using the ios::in mode. We then read a string from the file using the >> operator and display it on the screen.

Appending to a File

To append data to a file, you can open the file in append mode (ios::app). Here is an example of appending to a file:

#include <iostream>
using namespace std;
int main() {
fstream file;
file.open("data.txt", ios::app); // The ios::app mode opens the file for appending
if (!file) {
cout << "Error opening file!" << endl;
return 1;
}
file << "This is a new line." << endl;
file.close();
return 0;
}

In this example, we open the file data.txt for appending using the ios::app mode. We then write the string "This is a new line." to the file and close it.

Closing a File

After you have finished reading from or writing to a file, you should close it using the close() member function. This ensures that any buffered data is written to the file and that the file is properly closed.

Here is an example of closing a file:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file;
file.open("data.txt", ios::out); // The ios::out mode opens the file for writing
if (!file) {
cout << "Error opening file!" << endl;
return 1;
}
file << "Hello, World!" << endl;
file.close(); // Close the file
return 0;
}

In this example, we open the file data.txt for writing, write the string "Hello, World!" to the file, and then close the file using the close() member function.