Ok, I'll go through it line by line:
ifstream InFile ("FamilyHistory.txt");
This constructs a new std::ifstream called InFile, and gives its constructor the path to the file to open. This constructor has an optional second parameter that, if not specified, automatically uses ios::in.
http://www.cplusplus.com/reference/iostream/ifstream/ifstream/
int ch;
This just makes an integer for the std::ifstream::get() function below.
while((ch = InFile.get()) != EOF)
First, we call the get() member function of InFile. This returns an integer, so we store it in the integer from earlier. The reason it returns an integer is because some of the OS flags (such as End Of File) are not in the character range 0 to 255 or -128 to 127 - and with good reason, because then we'd have no way to tell apart file data from a special system flag. After the assignment, we compare the value of the integer to the EOF flag so that the loop keeps going until we reach the End of File system flag.
http://www.cplusplus.com/reference/iostream/istream/get/
There is also a specific function that can tell you if you've reached the end of the file, but I did not use it here:
http://www.cplusplus.com/reference/iostream/ios/eof/
1 2 3
|
{
cout << char(ch);
}
|
Inside the while loop, we cast the integer to a character so that when we print it out it will print out characters like "hello" and not numbers.
Also, the destructor of std::ifstream automatically closes the file for us, so we don't have to do that.