04 - File Streams - Engineering 101 Quote of the Day Be not...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Be not ashamed of mistakes and thus make them crimes. - Confucius File Streams The fstream Library If we include the fstream library by typing #include <fstream> <fstream> then we can define new streams that can get input from, and direct output to, files. fstream adds two new types: ifstream and ofstream. ofstream. These stand for input file stream and output file stream respectively. An ifstream is a sub-type of istream and ofstream is a subsub-type of ostream so they can be used just like cin suband cout. cout. The fstream Library istream cin ifstream ostream cout ofstream operations >> operations << Reading From Files 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; Writing To Files 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"); 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; 1 The fstream Library istream cin thisfile.txt The fstream Library x 25 istream y 15 thisfile.txt ostream cout ofstream ostream cout ofstream cin ifstream infile 25 15 ifstream infile 25 15 operations << operations >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; operations >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; operations << The fstream Library x 25 istream y 15 thisfile.txt The fstream Library x 25 istream y 15 thatfile.txt thisfile.txt ostream cout ofstream outfile operations << ofstream outfile("thatfile.txt"); outfile << "x =" << x << endl; outfile << "y =" << y << endl; ostream cout ofstream outfile operations << ofstream outfile("thatfile.txt"); outfile << "x =" << x << endl; outfile << "y =" << y << endl; thatfile.txt cin ifstream infile cin ifstream infile 25 15 25 15 x = 25 operations >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; operations >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; The fstream Library x 25 istream y 15 thisfile.txt Opening and Closing Files Actually the line ifstream infile("thisfile.txt"); does two things: thatfile.txt ostream cout ofstream outfile operations << ofstream outfile("thatfile.txt"); outfile << "x =" << x << endl; outfile << "y =" << y << endl; cin ifstream infile 25 15 x = 25 y =15 It declares a new input file stream called infile It opens the file "thisfile.txt" and associates it with this stream operations >> ifstream infile("thisfile.txt"); int x, y; infile >> x >> y; This can be done in two separate steps ifstream infile; infile.open("thisfile.txt"); 2 Opening and Closing Files When we are done using a file it is good practice to close the file by a line such as: ifstream infile; infile.open("thisfile.txt"); infile >> x >> y; infile.close(); open and close are operations defined on ofstream and ifstream objects The fstream Library istream cin ifstream operations open, close operations >> ostream cout ofstream operations open, close operations << What is in outfile.txt after the first program executes? #include <iostream> #include <fstream> using namespace std; int main( ){ ofstream outfile("outfile.txt"); cerr << "Alpha" << endl; cout << "Beta" << endl; outfile << "Gamma" << endl; outfile.close(); return 0; } What is in outfile.txt after the second program executes? #include <iostream> #include <fstream> using namespace std; int main( ){ int x=4; ofstream outfile("outfile.txt"); outfile << x << x+1 << x+3; outfile << x-1 << endl; xoutfile.close(); return 0; } 1 Alpha 2 Beta 3 Gamma 4 Alpha Beta Gamma 1 4573 2 457 3 3 4587 4 458 7 What is in outfile.txt after the third program executes? #include <iostream> #include <fstream> using namespace std; void printproc(ofstream & zfile, int a){ zfile << a << a << endl; return; } int main( ){ ofstream outfile("outfile.txt"); printproc(outfile, 7590); outfile.close(); return 0; } What is output to the screen after the fourth program executes? int main( ){ int a, b, c, d, e, f; ofstream outfile("outfile.txt"); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile("outfile.txt"); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; } void permute(ofstream & fout, int a, int b, int c) { fout << a << b << c << endl; fout << a << c << b << endl; fout << b << a << c << endl; fout << b << c << a << endl; fout << c << a << b << endl; fout << c << b << a << endl; return; } 1 2 7590 3 75907590 4 7590 7590 1 21 32 13 2 31 23 12 3 312132 231213 312132 4 321123 312132 231213 3 Next Lecture Iteration and Conditionals 4 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Winter '07 term at University of Michigan.

Ask a homework question - tutors are online