ENGR 101 Lecture 16: File Streams
Document Summary
Engr 101: introduction to computers and algorithms - lecture 16: file streams. If we include the fstream library by typing: #include then we can define new streams that can get input from, and direct output to, files. Fstream adds two new types: ifstream and ofstream. These stand for input file stream and output file stream respectively. An ifstream is a sub-type of istream and ofstream is a sub-type of of ostream so they can be used just like cin and cout. To read from a file you first must declare a new ifstream and open it. This is done with a line of the form: ifstream infile( thisfile. txt ); This line will open a file called thisfile. txt and create a new ifstream called infile to extract input from the file by lines like: infile >> x >> y; To write to a file you first must declare a new ofstream and open it.