课程: Complete Guide to C++ Programming Foundations
免费学习该课程!
今天就开通帐号,24,100 门业界名师课程任您挑!
Opening a text file for reading - C++教程
课程: Complete Guide to C++ Programming Foundations
Opening a text file for reading
- [Instructor] Let me show you how to read text files. For that, the standard library provides the ifstream 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 that 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's 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 from the file. Then in line 15, I open a file with the open member function of inFile. The file is called players.txt, and you can find it in the same folder as this exercise file. After successfully running this open function, we can use inFile to read that file we just opened. But this function may run into an error, so we must check if everything went well. That's why in line 16, I am using the fail function to check if the open function failed. If it did…