Learn how to open files and read data from them using ifstream.
Reading data from a file is accomplished using the `ifstream` class. The process is symmetric to writing. First, you create an `ifstream` object and open the desired file for reading: `std::ifstream inFile("example.txt");`. You should then verify that the file was opened successfully using `inFile.is_open()`. If the file doesn't exist or can't be opened, the operation will fail. There are several ways to read data. You can use the stream extraction operator (`>>`) to read formatted, whitespace-separated data, similar to `std::cin`. For example, if your file contains `100 Hello`, `inFile >> myInt >> myString;` would read `100` into `myInt` and `Hello` into `myString`. A more common task is to read a file line by line. This is done using the `std::getline()` function. You typically use a `while` loop to read all the lines from a file: `while (std::getline(inFile, line)) { ... }`. This loop will continue as long as `getline` is able to successfully read a line from the file into the `line` string variable. Once you're finished reading, you should close the file using `inFile.close()`. Just like `ofstream`, the `ifstream` object's destructor will also close the file automatically when it goes out of scope, providing a safer way to manage the file resource.