ifstream myfile("example.txt"); if (myfile.fail()) { cout << "The file cannot be opened."; }
ifstream myfile("example.txt"); string line; if (myfile.is_open()) { while (getline(myfile, line)) { cout << line << endl; } } else { cout << "The file cannot be opened."; }In this example, the `getline` function reads the file line by line until it has been fully read. `is_open()` is a related function that checks if the file has been opened successfully. The `ifstream` function belongs to the C++ standard library.