how to open and read text files in c++

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

int main() {
    // Step 1: Declare a file stream object
    std::ifstream inputFile;

    // Step 2: Open the file
    inputFile.open("example.txt");

    // Step 3: Check if the file is successfully opened
    if (!inputFile.is_open()) {
        std::cerr << "Error opening file!" << std::endl;
        return 1;
    }

    // Step 4: Read the file line by line
    std::string line;
    while (std::getline(inputFile, line)) {
        // Step 5: Process each line (e.g., print it)
        std::cout << line << std::endl;
    }

    // Step 6: Close the file
    inputFile.close();

    return 0;
}