8.1) File Input/Output (I/O) in C++

File Input/Output (I/O) in C++ allows you to read and write data to and from files on the system. C++ provides a set of standard classes and functions for performing file I/O operations. File I/O is essential for tasks such as reading configuration files, processing large datasets, and storing program output persistently.

The main components of file I/O in C++ are:

Stream Classes (fstream, ifstream, ofstream)


C++ provides three stream classes for working with files:

  • ifstream (input file stream): Used for reading data from files.
  • ofstream (output file stream): Used for writing data to files.
  • fstream (file stream): Combines both input and output capabilities.
  1. Opening and Closing Files:
    To perform file I/O, you need to open a file. The open() function is used for this purpose. After you are done with the file, you should close it using the close() function to release system resources.
  2. Reading from Files:
    You can read data from files using various methods provided by the stream classes, such as getline() to read lines and >> operator to read formatted data.
  3. Writing to Files:
    You can write data to files using methods such as write() and the << operator.
  4. Checking for Errors:
    Stream classes provide functions like good(), fail(), and eof() to check the state of the stream and handle errors.

Here’s a simple example of file I/O in C++:

#include <iostream>
#include <fstream>

int main() {
    std::ofstream outputFile("output.txt"); // Open for writing

    if (outputFile.is_open()) {
        outputFile << "Hello, File I/O!" << std::endl;
        outputFile.close(); // Close the file
    } else {
        std::cerr << "Failed to open the file for writing." << std::endl;
    }

    std::ifstream inputFile("output.txt"); // Open for reading

    if (inputFile.is_open()) {
        std::string line;
        getline(inputFile, line);
        std::cout << "Read from file: " << line << std::endl;
        inputFile.close(); // Close the file
    } else {
        std::cerr << "Failed to open the file for reading." << std::endl;
    }

    return 0;
}

Explanation of the code:

  • The ofstream is used to open the file "output.txt" for writing. The << operator is used to write data into the file, and the close() function is called to close the file.
  • The ifstream is used to open the same file for reading. The getline() function is used to read a line from the file, and the result is printed to the console.

Output:

Read from file: Hello, File I/O!

In this example, the file "output.txt" is created and opened for writing. The message is written into the file, and then the file is closed. Later, the same file is opened for reading, and the content is read and displayed on the console.

File I/O is a fundamental concept in programming and is used in a wide range of applications. It’s important to handle file opening, reading, and writing carefully and to check for errors to ensure robust behavior in your programs.

Leave a Reply