{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04 - File Streams - Full - Engineering 101 File Streams...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering 101 File Streams
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Quote of the Day - Confucius Be not ashamed of mistakes and thus make them crimes.  
Image of page 2
The fstream Library n If we include the fstream library by typing #include <fstream> then we can define new streams that can get input  from, and direct output to, files. n fstream  adds two new types:  ifstream  and  ofstream . n These stand for input file stream and output file  stream respectively. n An  ifstream  is a sub-type of  istream  and  ofstream  is a  sub-type of  ostream  so they can be used just like  cin   and  cout .
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
More About Streams n There is another output stream we can access  through the  iostream  library:   cerr n cerr  stands for “standard error” n cout   is  buffered n The output does not go directly to the screen. n cerr   is not  buffered n cout  is tied to  cin n Whenever  cin  is used the  cout  buffer is “flushed”
Image of page 4
More About Streams You (the user) The computer OS Your Executable cin cout cerr
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ostream cout, cerr operations << The fstream Library istream cin operations >> ifstream ofstream
Image of page 6
Reading From Files n 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”); n 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;
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Writing To Files n To write to a file you first must declare a new  ofstream and open it.  This is done with a line of the  form: ofstream outfile(“thatfile.txt”); n This line will open a file called thatfile.txt and create  a new ofstream called outfile to send output to the file  by lines like: outfile << “x =” << x << “ and y =” << y;
Image of page 8
The fstream Library istream cin operations >> ostream cout, cerr operations << ifstream infile ofstream ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; 25 15 thisfile.txt
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ostream cout, cerr operations << The fstream Library istream cin operations >> ifstream infile ofstream ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; 25 15 thisfile.txt x 25 y 15
Image of page 10
ostream cout, cerr operations << The fstream Library istream cin operations >> ifstream infile ofstream outfile ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; ofstream outfile(“thatfile.txt”); outfile << “x =” << x << endl; outfile << “y =” << y << endl; 25 15 thisfile.txt thatfile.txt x 25 y 15
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ostream cout, cerr operations << The fstream Library istream cin operations >> ifstream infile ofstream outfile ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; ofstream outfile(“thatfile.txt”); outfile << “x =” << x << endl; outfile << “y =” << y << endl; 25 15 thisfile.txt x = 25 thatfile.txt x 25 y 15
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}