#include#include int main() { std::ifstream inputFile("example.txt"); if (inputFile.bad()) { std::cout << "Error opening file\n"; return 1; } // code to read data from the file return 0; }
#includeIn this example, the `std::ifstream` class is used to open a file named "example.txt". The data is read from the file using the `>>` operator and stored in the variable `data`. The `bad()` function is used to check if any error occurred while reading the data. If there is an error, the program displays an error message and exits. The `std::ifstream` class and its member functions belong to the C++ Standard Library.#include int main() { std::ifstream inputFile("example.txt"); int data; while (inputFile >> data) std::cout << data << " "; if (inputFile.bad()) { std::cout << "Error reading file\n"; return 1; } // code to process the data read from the file return 0; }