From the course: Learning C++
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Opening a text file for reading - C++ Tutorial
From the course: Learning C++
Opening a text file for reading
- [Instructor] Now let me show you how to read text files. For that, the standard library provides the <fstream> header, which I've included in line seven. This header is a part of the input output library and it provides a number of classes to handle files. In line 10, we have the declaration of an ifstream object called inFile. The ifstream class is an input stream from a file, and so, it is very similar to the cin object. Next, we have the declarations for a string, an integer, and a char, which will be used to store values coming in from the file. Then in line 15, I open a file with the open member function of inFile. The file is called people.txt and you can find it in the same folder as this exercise file. After successfully running this function, we can use inFile to read that file we just opened, but this function may run into a error, so we must check if everything went well. That's why in line 16, I'm using…