std::ifstream is C++'s standard input file stream. You can learn about file streams from the tutorial on this site:
http://www.cplusplus.com/doc/tutorial/files/
The tutorial is slightly outdated - for example, you almost never need to use
.open()
or
.close()
.
Kreadeth wrote: |
---|
In the line 8, you used 'in' as though it is a boolean function and in code 11 it is used in a different syntax. What does it mean? |
This is how all C++ stream classes work - all C++ streams are implicitly convertible to boolean type, and they all support the formatted extraction/insertion operators (depending on whether they are an input stream or output stream, of course).
On line 8, I both construct the stream and convert it to boolean to check if it is valid to use. If something went wrong with opening the file, the code will go to the else branch.
On line 11 I just use the formatted extraction operator the same way I would with
std::cin
. In C++ it is proper to loop on the input operation. When the input operation fails, the stream's implicit boolean becomes false and ends the while loop.
Kreadeth wrote: |
---|
Why we need "word" string? |
It is just a temporary variable - we cannot directly take from a string and add to a vector, we need the temporary variable.