c++ read matttrix from text file

#include <iostream>
#include <fstream>
#include <vector>

int main() {
    std::ifstream file("matrix.txt");  // Step 1: Open the text file for reading

    if (file.is_open()) {  // Step 2: Check if the file is successfully opened
        int rows, cols;
        file >> rows >> cols;  // Step 3: Read the number of rows and columns from the file

        std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));

        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                file >> matrix[i][j];  // Step 4: Read matrix elements from the file
            }
        }

        file.close();  // Step 5: Close the file

        // Matrix is now stored in the 'matrix' variable
    } else {
        std::cerr << "Unable to open the file." << std::endl;  // Step 2 (alternative): Handle file opening failure
    }

    return 0;
}