Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.

Similar presentations


Presentation on theme: "Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson."— Presentation transcript:

1 Programming File I/O

2 COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. Files in a personal computer environment

3 COMP102 Prog. Fundamentals File I/O / Slide 3 Using Input/Output Files l 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 to a program or receive output data from a program, or both; n must reside in Project directory (not necessarily the same directory as the.cpp files) n must be opened before it is used.

4 COMP102 Prog. Fundamentals File I/O / Slide 4 Using Input/Output Files l 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).

5 COMP102 Prog. Fundamentals File I/O / Slide 5 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. C++ streams

6 COMP102 Prog. Fundamentals File I/O / Slide 6 Input File-Related Functions #include ifstream fsIn; l fsIn.open("fname.txt") connects stream fsIn to the external file " fname.txt ". l fsIn.close() n disconnects the stream and associated file. fsIn >> c; //Behaves just like cin

7 COMP102 Prog. Fundamentals File I/O / Slide 7 Output File-Related Functions #include ofstream fsOut; l fsOut.open("fname.txt") connects stream fsOut to the external file " fname.txt ". l fsOut.close() n disconnects the stream and associated file. fsOut << c; //Behaves just like cout

8 COMP102 Prog. Fundamentals File I/O / Slide 8 File I/O: Example 1 // Reads three numbers from the file numbers.dat, // sums the numbers, and writes the sum to the file // sum.dat. #include // for cin, cout #include // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.dat"); // open the output file output_stream.open("sum.dat");

9 COMP102 Prog. Fundamentals File I/O / Slide 9 File I/O: Example 1 // declare variables and read in the data (3 numbers) int first, second, third; input_stream >> first >> second >> third; // write the data to the output file output_stream << "The sum of the first 3\n" << "numbers in input_file.dat\n" << "is " << (first + second + third) << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0; }

10 COMP102 Prog. Fundamentals File I/O / Slide 10 File I/O: Example 1.2 // copy everything from input.txt to output.txt #include // for cin, cout #include // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open(“input.txt"); // open the output file output_stream.open(“output.txt");

11 COMP102 Prog. Fundamentals File I/O / Slide 11 File I/O: Example 1.2 char next; input_stream.get(next); while (! input_stream.eof()) { cout << next; output_stream.put(next); input_stream.get(next); } input_stream.close(); output_stream.close(); return 0; }

12 COMP102 Prog. Fundamentals File I/O / Slide 12 File I/O: Example 1.3 // Reads all the numbers from the file numbers.dat, // sums the numbers, and writes the sum and average to // the file sum.dat. #include // for cin, cout #include // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.txt"); // open the output file output_stream.open("sum.dat");

13 // declare variables and read in the data int first, sum =0, counter= 0; input_stream >> first; while (!input_stream.eof()){ //test if is endoffile sum +=first; counter++; input_stream >> first; } cout << "The sum of numbers in the file is "<< sum << endl; cout << "The average is " << double(sum)/counter << endl; // write the data to the output file output_stream << "The sum is " << sum << endl; output_stream << "The average is “ << double(sum)/counter << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0; }

14 COMP102 Prog. Fundamentals File I/O / Slide 14 Example 2 //copies indata.dat to outdata.dat and counts the number of lines. Prints //file to screen too. #include using namespace std; int main(){ ifstream ins; ofstream outs; int count=0; char next; ins.open("indata.dat");// open the input file outs.open("outdata.dat");// open the output file

15 COMP102 Prog. Fundamentals File I/O / Slide 15 count=0; while (ins.get(next)) { if (next=='\n') { count++; } cout << next; outs << next; } if (next !='\n') {// when last line not end with ‘/n’ count++; cout << endl; outs << endl; } ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl; } Example 2

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.

17 COMP102 Prog. Fundamentals File I/O / Slide 17 File I/O: Example 3 // Counts the number of blanks on each line of the file // indata1.txt #include using namespace std; int main(){ ifstream ins; int count; char next; ins.open("indata1.txt");// open the file ins.get(next);// get the first char

18 COMP102 Prog. Fundamentals File I/O / Slide 18 File I/O: Example 3 while(!ins.eof()){// loop to read each line count = 0; while(next!='\n' ){ cout << next; if(next==' ') count++; ins.get(next); } cout << endl << "Blanks: " << count << endl; ins.get(next); } ins.close(); return 0; }

19 COMP102 Prog. Fundamentals File I/O / Slide 19 Summary of Input File-Related Functions #include ifstream fsIn; l fsIn.open(const char[] fname) connects stream fsIn to the external file fname. l fsIn.get(char& c) extracts next character from the input stream fsIn and places it in the character variable c. l fsIn.eof() n tests for the end-of-file condition. l fsIn.close() n disconnects the stream and associated file. fsIn >> c; //Behaves just like cin

20 COMP102 Prog. Fundamentals File I/O / Slide 20 Summary of Output File-Related Functions #include ofstream fsOut; l fsOut.open(const char[] fname) connects stream fsOut to the external file fname. l fsOut.put(char c) inserts character c to the output stream fsOut. l fsOut.eof() n tests for the end-of-file condition. l fsOut.close() n disconnects the stream and associated file. fsOut << c; //Behaves just like cout


Download ppt "Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson."

Similar presentations


Ads by Google