Uniform initialization with std::ofstream in C++

When working with file I/O in C++, the std::ofstream class is commonly used to write data to files. In C++11 and later, you can utilize the uniform initialization syntax to create an std::ofstream object and open the file in a single line of code.

Let’s see how uniform initialization can be used with std::ofstream:

#include <fstream>

int main() {
    std::ofstream outputFile{"output.txt"};
    if (outputFile.is_open()) {
        outputFile << "Hello, World!";
        outputFile.close();
        std::cout << "File successfully written." << std::endl;
    } else {
        std::cout << "Failed to open file." << std::endl;
    }
    return 0;
}

In the above example, we create an std::ofstream object named outputFile and provide the filename "output.txt" as an initializer. The file is opened in the constructor itself. Note that the file will be opened in the mode specified by the default constructor of std::ofstream, which is std::ios_base::out (output mode).

We then check if the file is successfully opened using the is_open() member function. If the file is open, we can write data to it using the << operator. In this case, we write the string "Hello, World!".

After writing data, we close the file using the close() member function. It is always a good practice to close the file once we have finished writing to it.

Finally, we provide appropriate output messages indicating whether the file was written successfully or if there was an error opening the file.

Uniform initialization with std::ofstream simplifies the code by combining object creation and file opening into a single statement. It enhances readability and reduces the chances of error by eliminating the need for separate lines to create and open the file.

Remember to include the <fstream> header to use the file stream classes in C++.

Give it a try and leverage the power of uniform initialization with std::ofstream in your C++ file I/O operations!

References:

#C++ #FileIO