Download presentation
Presentation is loading. Please wait.
1
File I/O
2
COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent n can be used to provide input data or receive output data, or both n must reside in Project directory (not necessarily the same directory as the.cpp files) n must be opened before reading it
3
COMP104 Lecture 20 / Slide 3 Using Input/Output Files * stream - a sequence of characters n interactive (iostream) cin - input stream associated with keyboard cout - output stream associated with display n file (fstream) ifstream - defines new input stream (normally associated with a file) ofstream - defines new output stream (normally associated with a file)
4
COMP104 Lecture 20 / Slide 4 Two names The external file name: keyboard, screen, something.dat The internal stream (or logical) name: cin, cout, stream-name open
5
COMP104 Lecture 20 / Slide 5 Basic operations #include ifstream xxx; ofstream yyy; xxx.open(filename1); yyy.open(filename2); xxx >> … >> … yyy << … << … xxx.close(); yyy.close();
6
COMP104 Lecture 20 / Slide 6 > in file I/O You can read and write integers, doubles, chars, etc. from files just like cin >> and cout << !
7
COMP104 Lecture 20 / Slide 7 Example 1 file1.dat: 1 2 3 4(eof) file2.dat: 4 3 2 1 (eof) Write a program which copies file1.dat to file2.dat.
8
COMP104 Lecture 20 / Slide 8 #include using namespace std; void main(){ ifstream fin; int A[4], r; fin.open("file1.dat");// read data file of four integers for(r=0; r<4; r++)// into array fin >> A[r]; fin.close(); ofstream fout; fout.open("file2.dat"); // write data file for(r=3; r>=0; r--)// with numbers reversed fout << A[r] << ' '; fout.close(); }
9
COMP104 Lecture 20 / Slide 9 Character I/O #include ifstream xxx; char ch; * xxx.open(fname) connects stream xxx to the external file fname * xxx.get(ch) Gets the next character from the input stream xxx and places it in the character variable ch * xxx.put(ch) Puts the character ch into the output stream xxx * xxx.eof() n tests for end-of-file condition * xxx.close() n disconnects the stream and closes the file Remark: two ‘invisible characters’, ‘\n’ (end of the line) and the end-of-file
10
COMP104 Lecture 20 / Slide 10 A typical usage: ifstream ins; char ch; ins.get(ch); while (!ins.eof()) { cout << ch; ins.get(ch); } while (ins.get(ch)) { cout << ch; }
11
COMP104 Lecture 20 / Slide 11 Example 2 indata.dat: a b c top10 methods to count spaces 1 3(eof) outdata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: a b c top10 methods to count spaces 1 3 Number of lines copied: 4 Write a program which copies indata.dat to outdata.dat and counts the number of lines. Prints file to screen too.
12
COMP104 Lecture 20 / Slide 12 Solution to example 2 #include using namespace std; void main(){ ifstream ins; ofstream outs; int count=0; char ch; ins.open("indata.dat");// open the input file outs.open("outdata.dat");// open the output file
13
COMP104 Lecture 20 / Slide 13 count=0; ins.get(ch); while (!ins.eof()) { if (ch=='\n') { count++; cout << endl; outs << endl;} else { cout << ch; outs << ch;} ins.get(ch); } count++; ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl; } The last line does not have a ‘\n’!
14
COMP104 Lecture 20 / Slide 14 More compact C++ style count=0; while (ins.get(ch)) { if (ch=='\n') { count++; cout << endl; outs << endl;} else { cout << ch; outs << ch;} } count++; cout << endl; outs << endl;
15
COMP104 Lecture 20 / Slide 15 Final refined solution! count=0; while (ins.get(next)) { if (next=='\n') { count++; cout << endl; outs << endl;} else { cout << next; outs << next;} } if (next != ‘\n’) { count++; cout << endl; outs << endl; } The previous solutions do not handle the case of empty file. File I/O is messy, use it moderately now!!!
16
COMP104 Lecture 20 / Slide 16 Example 3 indata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: a b c Blanks: 2 top10 methods to count spaces Blanks: 4 Blanks: 0 1 3 Blanks: 3 Write a program which counts the number of blanks on each line of indata.dat, outputs each line, and number of blanks on each line.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.