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