1 / 16

File I/O

File I/O. 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)

pariswelsh
Download Presentation

File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. file1.dat: 1 2 3 4(eof) file2.dat: 4 3 2 1 (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 streamxxxto the external filefname • 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 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; ins.get(ch); } while (ins.get(ch)) { cout << ch; }

  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. Solution to example 2 #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. 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. 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. 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. 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: a b c Blanks: 2 top10 methods to count spaces Blanks: 4 Blanks: 0 1 3 Blanks: 3

More Related