We have been using the iostream standard library, which supports, among other functionalities, cin for reading from standard input (usually the keyboard), and cout for outputting to standard output (usually the monitor).
Reading or writing from a file requires another standard library, fstream. The fstream standard library is included with the statement:
Both iostream and fstream have in common the word stream. This is no accident . Both standard libraries concern streams of bytes. The iostream library concerns streams of bytes resulting from the io in iostream, input and output. The fstream standard library concerns streams of bytes resulting from the f in fstream, a file.
The fstream header file defines three new data types:
ofstream This data type represents the output file streamthe o in ofstream standing for output. The direction of output is from your program out to a file. The ofstream data type is used to create files and to write information to files. It cannot be used to read files.
ifstream This data type represents the input file streamthe i in ifstream standing for input. The direction of input is from a file into your program. The ifstream data type is used to read information from files. It cannot be used to create files or to write information to them.
fstream This data type represents the file stream generally , and has the capabilities of both ofstream and ifstream . It can create files, write information to files, and read information from files.