1 / 10

File I/O (Ch. 7)

Programming. File I/O (Ch. 7). Using Input/Output Files. A computer file is stored on a secondary storage device (e.g., disk) is permanent until overwritten can be used to provide input data or receive output data, or both

sloan
Download Presentation

File I/O (Ch. 7)

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

  2. Using Input/Output Files • A computer file • is stored on a secondary storage device (e.g., disk) • is permanent until overwritten • 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. Declaring ifstream and ofstream Input File: (ifstream) ifstream fin; // declare input stream fin.open("data.txt"); // open the file fin >> varName; // reads a data from file // into variable “varName” // variable <type> matters! fin.close(); // closes the file Output File: (ofstream) ofstream fout; // declare output stream fout.open("data.txt");// open the file fout << varName; // writes the data in variable // “varName” to file. // variable <type> matters! fout.close(); // closes the file

  5. Example 1 – input from file You can read and write integers, doubles, chars, etc. from files just like cin >>and cout <<: #include <iostream.h> #include <fstream.h> void main(void) { int data; ifstreamfin; fin.open("data.txt"); fin >> data; while (fin) // fin==false means no more data { cout << data << endl; fin >> data; } fin.close(); } Open the file before you use it Close the file after using it

  6. data.txt 10 20 30 10 20 30 [EOF] File I/O: Example 1 • [EOF] is a special character: End Of File • When the file opens, the reading position is at the beginning • Every time you read a number, the reading position advances • When you read [EOF], fin becomes false If you want to use a DOS path name, you must put TWO backslashes fin.open(“d:\\MyData\\data.txt");

  7. Example 2: output to file In the same way you can write data to a file #include <iostream.h> #include <fstream.h> void main(void) { int i; ofstreamfout; fout.open(“data2.txt"); fout << “Compute x^2” << endl; for (i=1; i<=3; i++) fout << i << “ “ << i*i << endl; fout.close(); } Compute x^2 1 1 2 4 3 9 Open with NotePad data2.txt Compute x^2 [CR] 1 1 [CR] 2 4 [CR] 3 9 [CR] [EOF] Inserted by the OS

  8. File I/O: Example 3 - input #include <fstream.h> void main(void) { int i, size; int array[100] = {0}; ifstreamfin; fin.open("data.txt"); fin >> size; // read in number of data pts for (i=0; i<size; i++) fin >> array[i]; // read in elements fin.close(); // ........ use the array ........ // calculate average, etc . . . } Input an 1-D array of int The first element of the file is the size of the array, and then, the actual data. Example: data.txt 5 10 12 14 10 18

  9. Checking for errors .fail() After opening a file • It is a good idea to make sure it was opened properly. • We can do this using the “.fail()” method Example: #include <iostream.h> #include <fstream.h> // input from file Void main() { ifstreamfin; // input file name is fin fin.open(“test.dat”); if (fin.fail()) // check if the open failed! { cout << “Error opening file \n”; } }

  10. Common Mistakes to Avoid Remeber to open the file! Common mistake ifstream fin; fin >> size; // error!!!! didn’t open the file Correct ifstream fin; fin.open(“name-of-file”); fin >> size; // error!!!! didn’t open the file Close file only after done read/writing out data (not before) ofstream fout; fout.open(“name-of-file”); fout << size; // OK fout.close(); // Closed file . . . fout << more_data; // error!!!! file has been closed!

More Related