Presentation is loading. Please wait.

Presentation is loading. Please wait.

This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.

Similar presentations


Presentation on theme: "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."— Presentation transcript:

1 This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000. 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 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 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 void main(void) { int data; ifstream fin; 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 File I/O: Example 1 If you want to use a DOS path name, you must put TWO backslashes fin.open(“d:\\MyData\\data.txt"); 10 20 30 [EOF] data.txt 10 20 30  [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

7 Example 2: output to file In the same way you can write data to a file #include void main(void) { int i; ofstream fout; 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 [CR] 1 1 [CR] 2 4 [CR] 3 9 [CR] [EOF] data2.txt Open with NotePad Compute x^2 1 2 4 3 9 Inserted by the OS

8 File I/O: Example 3 - input #include void main(void) { int i, size; int array[100] = {0}; ifstream fin; 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 #include // input from file Void main() { ifstream fin; // 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!


Download ppt "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."

Similar presentations


Ads by Google