04 - File Streams - Full

04 File Streams- - Engineering 101 File Streams Quote of the Day Be not ashamed of mistakes and thus make them crimes Confucius The fstream Library

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

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

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

View Full DocumentRight Arrow Icon
Quote of the Day - Confucius Be not ashamed of mistakes and thus make them crimes.  
Background 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 .
Background image of page 3

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

View Full DocumentRight 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”
Background image of page 4
More About Streams You (the user) The computer OS Your Executable cin cout cerr
Background image of page 5

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

View Full DocumentRight Arrow Icon
ostream cout, cerr operations << The fstream Library istream cin operations >> ifstream ofstream
Background 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;
Background image of page 7

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

View Full DocumentRight 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;
Background 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
Background image of page 9

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

View Full DocumentRight 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
Background 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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

Page1 / 39

04 File Streams- - Engineering 101 File Streams Quote of the Day Be not ashamed of mistakes and thus make them crimes Confucius The fstream Library

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online