Lecture+08 - Engineering 101 Engineering 101 Lecture 8...

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 Engineering 101 Lecture 8 9/27/07 File Streams Quote of the Day Quote of the Day Be not ashamed of mistakes and thus make them crimes. ­ Confucius More About Streams More About Streams We already introduced input streams and output streams in the context of talking about cin and cout. cin is standard input and we have used this to get input from the keyboard. cout is standard output and we have used this to send output to the screen. More About Streams More About Streams cin You (the user) The computer OS Your Executable cout More About Streams More About Streams There are two other output streams we can access through the iostream library. cerr is standard error. clog is standard log. cout and clog are buffered, the output does not go directly to the screen. cerr is not buffered. (see next slide for a visual representation) cout is tied to cin. Whenever cin is used the cout buffer is “flushed”. More About Streams More About Streams cin You (the user) The computer OS Your Executable cerr cout clog Using Unix (Linux) You Can Using Unix (Linux) You Can Redirect I/O To Files red% myprogram < inputfilename Reads the input from an input file instead of the keyboard. You can choose any filename. red% myprogram > outputfile Sends the output to an output file instead of the screen. Again the filename is up to you. red% myprogram < inputfile > outputfile Better Ways To Use Files Better Ways To Use Files This trick of piping the standard output to a file or the standard input from a file will not work if you want to communicate using the keyboard, screen AND files. 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. The fstream Library The fstream Library 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 ostream so they can be used just like cin and cout. The fstream Library The fstream Library istream cin ifstream operations >> ostream cout clog cerr ofstream operations << Reading From Files 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 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; The fstream Library The fstream Library istream cin thisfile.txt 25 15 ifstream infile ostream cout clog cerr ofstream operations << operations >> ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; The fstream Library The fstream Library x 25 istream y 15 thisfile.txt cin ifstream infile ostream cout clog cerr ofstream operations << 25 15 operations >> ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; The fstream Library The fstream Library x 25 istream y 15 thisfile.txt cin ifstream infile 25 15 ostream cout clog cerr ofstream outfile operations << thatfile.txt operations >> ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; ofstream outfile(“thatfile.txt”); outfile << “x =” << x << endl; outfile << “y =” << y << endl; The fstream Library The fstream Library x 25 istream y 15 thisfile.txt cin ifstream infile 25 15 ostream cout clog cerr ofstream outfile operations << thatfile.txt x = 25 operations >> ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; ofstream outfile(“thatfile.txt”); outfile << “x =” << x << endl; outfile << “y =” << y << endl; The fstream Library The fstream Library x 25 istream y 15 thisfile.txt cin ifstream infile 25 15 ostream cout clog cerr ofstream outfile operations << thatfile.txt x = 25 y =15 operations >> ifstream infile(“thisfile.txt”); int x, y; infile >> x >> y; ofstream outfile(“thatfile.txt”); outfile << “x =” << x << endl; outfile << “y =” << y << endl; Opening and Closing Files Opening and Closing Files Actually the line ifstream infile(“thisfile.txt”); does two things: This can be done in two separate steps ifstream infile; infile.open(“thisfile.txt”); It declares a new input file stream called infile It opens the file “thisfile.txt” and associates it with this stream Opening and Closing Files 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 The fstream Library istream cin ifstream operations open, close operations >> ostream cout clog cerr 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; } 1 Alpha 2 Beta 3 Gamma 4 Alpha Beta Gamma 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; } 1 Alpha 2 Beta 3 Gamma 4 Alpha Beta Gamma Program 1 Program 1 #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; outfile.close(); return 0; } 1 4573 2 457 3 3 4587 4 458 7 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; outfile.close(); return 0; } 1 4573 2 457 3 3 4587 4 458 7 Program 2 Program 2 #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; outfile.close(); return 0; } Program 2 Program 2 #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; outfile.close(); return 0; } Program 2 Program 2 #include <iostream> #include <fstream> using namespace std; int main( ){ int x=4; ofstream outfile(“outfile.txt”); outfile << 4 << 5 << 7; outfile << x­1 << endl; outfile.close(); return 0; } Program 2 Program 2 #include <iostream> #include <fstream> using namespace std; int main( ){ int x=4; ofstream outfile(“outfile.txt”); outfile << 4 << 5 << 7; outfile << x­1 << endl; outfile.close(); return 0; } 457 Program 2 Program 2 #include <iostream> #include <fstream> using namespace std; int main( ){ int x=4; ofstream outfile(“outfile.txt”); outfile << x << x+1 << x+3; outfile << 3 << endl; outfile.close(); return 0; } 457 Program 2 Program 2 #include <iostream> #include <fstream> using namespace std; int main( ){ int x=4; ofstream outfile(“outfile.txt”); outfile << x << x+1 << x+3; outfile << 3 << endl; outfile.close(); return 0; } 4573 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; } 1 2 7590 3 75907590 4 7590 7590 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; } 1 2 7590 3 75907590 4 7590 7590 Program 3 Program 3 #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(); outfile return 0; } outfile.txt Program 3 Program 3 #include <iostream> #include <fstream> using namespace std; void printproc(ofstream & zfile, int a){ zfile << a << a << endl; return; a zfile } 7590 int main( ){ ofstream outfile(“outfile.txt”); printproc(outfile, 7590); outfile.close(); outfile return 0; } outfile.txt Program 3 Program 3 #include <iostream> #include <fstream> using namespace std; void printproc(ofstream & zfile, int a){ zfile << a << a << endl; return; a zfile } 7590 int main( ){ ofstream outfile(“outfile.txt”); printproc(outfile, 7590); outfile.close(); outfile return 0; } outfile.txt 75907590 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; } What is output to the screen after the fourth program executes? 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 21 32 13 2 31 23 12 3 312132 231213 312132 4 321123 312132 231213 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; } What is output to the screen after the fourth program executes? 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 21 32 13 2 31 23 12 3 312132 231213 312132 4 321123 312132 231213 Program 4 Program 4 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; } } Program 4 Program 4 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; } } 123 132 213 231 312 321 Program 4 Program 4 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; 123 132 213 231 312 321 a 123 c 213 e 312 b 132 d 231 f 321 } Program 4 Program 4 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; 123 132 213 231 312 321 a 123 c 213 e 312 b 132 d 231 f 321 321123 312132 231213 } Next Lecture Next Lecture Iteration and Conditionals ...
View Full Document

Ask a homework question - tutors are online