File I/O and streams in C++

File Input/Output (I/O) is an essential aspect of programming, allowing us to read data from files or write data to files. In C++, file I/O operations are handled through streams, which are sequences of characters that represent I/O devices like keyboards, screens, and files.

File Modes

Before performing any file operations, we need to open the file in the appropriate mode. C++ provides three file modes:

  1. Input mode (ios::in): Opens a file for reading input.
  2. Output mode (ios::out): Opens a file for writing output. If the file exists, the previous content will be overwritten. If it doesn’t exist, a new file will be created.
  3. Append mode (ios::app): Opens a file for appending new content at the end. If the file doesn’t exist, a new file will be created.

Opening and Closing Files

Opening a File

To open a file, we use the fstream class from the <fstream> header. Here’s an example of opening a file in output mode:

#include <fstream>

int main() {
    std::ofstream file;
    file.open("output.txt", std::ios::out);
    // additional file operations
    file.close();
    return 0;
}

In the code above, we instantiate an ofstream object called file. Then, we call the open() function to open the file in output mode. The first argument to open() is the filename, and the second argument is the file mode.

Closing a File

After performing the required file operations, it’s important to close the file using the close() function. This ensures that all the data is written to the file and frees up system resources associated with the file.

Reading from a File

To read data from a file, we use the ifstream class. Here’s an example of reading from a file:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("input.txt", std::ios::in);
    std::string line;

    if (file.is_open()) {
        while (std::getline(file, line)) {
            std::cout << line << std::endl;
        }
        file.close();
    } else {
        std::cout << "Unable to open file" << std::endl;
    }

    return 0;
}

In the code above, we instantiate an ifstream object called file. We pass the filename and the file mode as arguments to the constructor. Then, we check if the file is open using the is_open() function.

If the file is open, we read the contents line by line using std::getline(), and then display each line on the console.

Writing to a File

To write data to a file, we use the ofstream class. Here’s an example of writing to a file:

#include <iostream>
#include <fstream>

int main() {
    std::ofstream file("output.txt", std::ios::out);

    if (file.is_open()) {
        file << "Hello, World!" << std::endl;
        file.close();
    } else {
        std::cout << "Unable to open file" << std::endl;
    }

    return 0;
}

In this example, we instantiate an ofstream object named file. We pass the filename and the file mode as arguments to the constructor. If the file is open, we write the string "Hello, World!" to the file using the << operator.

Conclusion

File I/O and streams in C++ provide a convenient way to work with files, enabling us to read input from files and write output to files. Understanding how to open, read, and write files using streams is essential for handling data persistence and file manipulation in C++.