Download presentation
Presentation is loading. Please wait.
Published byJoanna York Modified over 9 years ago
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 should reside in Project directory for easy access; 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 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. File Open Result
9
COMP102 Prog. Fundamentals File I/O / Slide 9 Object and Member Functions Calling Object Member Function Member Function argument
10
COMP102 Prog. Fundamentals File I/O / Slide 10 Object and Member Functions input_stream.open("numbers.dat") Stream Variable Name Calling Object Dot Operator Member Function Name File Name
11
COMP102 Prog. Fundamentals File I/O / Slide 11 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 #include 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");
12
COMP102 Prog. Fundamentals File I/O / Slide 12 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; }
13
COMP102 Prog. Fundamentals File I/O / Slide 13 File I/O: Example 2 // Reads three numbers from the file input_file.dat, // sums the numbers, and writes the sum to the file // output_file.dat. #include // for cin, cout #include // for ifstream, ostream #include using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // declare the input and output file names char input_file_name[16], output_file_name[16];
14
COMP102 Prog. Fundamentals File I/O / Slide 14 File I/O: Example 2 // user supplies the input and // output file names cout << "input_stream.open(input_file_name)”; cout << "Enter the input file name " << "(maximum 15 characters):\n"; cin >> input_file_name; cout << "Enter the output file name " << "(maximum 15 characters):\n"; cin >> output_file_name; // open the input file input_stream.open(input_file_name); // open the output file output_stream.open(output_file_name);
15
COMP102 Prog. Fundamentals File I/O / Slide 15 File I/O: Example 2 // 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; }
16
COMP102 Prog. Fundamentals File I/O / Slide 16 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. File opening states
17
COMP102 Prog. Fundamentals File I/O / Slide 17 More Input File-Related Functions l ifstream fsin; l fsIn.open(const char[] fname) connects stream fsIn to the external file fname. l fsIn.get(char& character) extracts next character from the input stream fsIn and places it in the character variable character. l fsIn.eof() n tests for the end-of-file condition.
18
COMP102 Prog. Fundamentals File I/O / Slide 18 More Output File-Related Functions l ofstream fsOut; l fsOut.open(const char[] fname) connects stream fsOut to the external file fname. l fsOut.put(char character) inserts character character to the output stream fsOut. l fsOut.eof() n tests for the end-of-file condition.
19
COMP102 Prog. Fundamentals File I/O / Slide 19 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
20
COMP102 Prog. Fundamentals File I/O / Slide 20 File I/O: Example 3 while(!ins.eof()){// loop to read each line count = 0; while(next!='\n' && !ins.eof()){ cout << next; if(next==' ') count++; ins.get(next); } cout << endl << "Blanks: " << count << endl; if(!ins.eof()) ins.get(next); } ins.close(); return 0; }
21
COMP102 Prog. Fundamentals File I/O / Slide 21 File I/O: Example 3 indata1.txt: a b c top10 methods to count spaces 1 3 Output: a b c Blanks: 2 top10 methods to count spaces Blanks: 4 Blanks: 0 1 3 Blanks: 3
22
COMP102 Prog. Fundamentals File I/O / Slide 22 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. User files
23
COMP102 Prog. Fundamentals File I/O / Slide 23 File I/O: Example 4 // Copies contents of file CS1STU.DAT // to file CS1GRADE.DAT // and counts number of lines #include using namespace std; int main(){ ifstream fsStu; ofstream fsGrades; int count=0; char next; fsStu.open("CS1STU.DAT");// open the input file fsGrades.open("CS1GRADE.DAT");//open the output file
24
COMP102 Prog. Fundamentals File I/O / Slide 24 File I/O: Example 4 do{ // loop to copy each line fsStu.get(next); // get the first char on line while(next!='\n' && ! fsStu.eof()){ cout << next; fsGrades << next; fsStu.get(next); } if(!fsStu.eof()){ cout << endl; fsGrades << endl; } count++; }while(!fsStu.eof()); fsStu.close(); fsGrades.close(); cout << "Number of lines copied: " << count << endl; return 0;}
25
COMP102 Prog. Fundamentals File I/O / Slide 25 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
26
COMP102 Prog. Fundamentals File I/O / Slide 26 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.