creating and writing into txt file cpp

#include <iostream>
#include <fstream>

int main() {
    // Step 1: Open a file stream for writing
    std::ofstream outputFile("example.txt");

    // Step 2: Check if the file stream is open
    if (outputFile.is_open()) {
        // Step 3: Write data to the file
        outputFile << "Hello, World!" << std::endl;
        outputFile << "This is a sample text file created using C++." << std::endl;

        // Step 4: Close the file stream
        outputFile.close();

        // Step 5: Inform the user that the file has been written successfully
        std::cout << "File has been written successfully." << std::endl;
    } else {
        // Step 6: Inform the user if the file stream could not be opened
        std::cerr << "Error opening the file." << std::endl;
    }

    // Step 7: Return 0 to indicate successful execution
    return 0;
}