Вы находитесь на странице: 1из 26

Programming

File I/O

MP102 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

MP102 Prog. Fundamentals File I/O / Slide 3

Using Input/Output Files


qA

computer file

s is

stored on a secondary storage device (e.g., disk); s is permanent; s can be used to provide input data to a program or receive output data from a program, or both; s should reside in Project directory for easy access; s must be opened before it is used.

MP102 Prog. Fundamentals File I/O / Slide 4

Using Input/Output Files


q

stream - a sequence of characters


s interactive

(iostream)

cin - input stream associated with keyboard. cout - output stream associated with display.
s file

(fstream)

ifstream - defines new input stream (normally associated with a file). ofstream - defines new output stream (normally associated with a file).

MP102 Prog. Fundamentals File I/O / Slide 5

Copyright 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

C++ streams

MP102 Prog. Fundamentals File I/O / Slide 6

Input File-Related Functions


#include <fstream> ifstream fsIn;
q

fsIn.open("fname.txt")
s

connects stream fsIn to the external file "fname.txt ". disconnects the stream and associated file. //Behaves just like cin

q q

fsIn.close()
s

fsIn >> c;

MP102 Prog. Fundamentals File I/O / Slide 7

Output File-Related Functions


#include <fstream> ofstream fsOut;
q

fsOut.open("fname.txt")
s

connects stream fsOut to the external file "fname.txt". disconnects the stream and associated file. //Behaves just like cout

q q

fsOut.close()
s

fsOut << c;

MP102 Prog. Fundamentals File I/O / Slide 8

Copyright 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

File Open Result

MP102 Prog. Fundamentals File I/O / Slide 9

Object and Member Functions


Calling Object
argument argument

Member Function

Member Function

MP102 Prog. Fundamentals File I/O / Slide 10

Object and Member Functions


Stream Variable Name Member Function Name

input_stream.open("numbers.dat")
Dot Operator File Name

Calling Object

MP102 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 <iostream> // for cin, cout #include <fstream> // for ifstream, ostream #include <iostream> 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");

MP102 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; }

MP102 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 <iostream> // for cin, cout #include <fstream> // for ifstream, ostream #include <iostream> 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];

MP102 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);

MP102 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; }

MP102 Prog. Fundamentals File I/O / Slide 16

File opening states

Copyright 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

MP102 Prog. Fundamentals File I/O / Slide 17

More Input File-Related Functions


ifstream fsin; q fsIn.open(const char[] fname)
q
s

connects stream fsIn to the external file fname. extracts next character from the input stream fsIn and places it in the character variable character. tests for the end-of-file condition.

fsIn.get(char& character)
s

fsIn.eof()
s

MP102 Prog. Fundamentals File I/O / Slide 18

More Output File-Related Functions


ofstream fsOut; q fsOut.open(const char[] fname)
q
s

connects stream fsOut to the external file fname. inserts character character to the output stream fsOut. tests for the end-of-file condition.

fsOut.put(char character)
s

fsOut.eof()
s

MP102 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 <iostream> #include <fstream> using namespace std;

int main(){ ifstream ins; int count; char next; ins.open("indata1.txt"); ins.get(next); // open the file // get the first char

MP102 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;

MP102 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

MP102 Prog. Fundamentals File I/O / Slide 22

User files

Copyright 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

MP102 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 <iostream> #include <fstream> 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

MP102 Prog. Fundamentals File I/O / Slide 24

File I/O: Example 4


// 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;} do{

MP102 Prog. Fundamentals File I/O / Slide 25

Summary of Input File-Related Functions

#include <fstream> ifstream fsIn;


q q

fsIn.open(const char[] fname)


s

connects stream fsIn to the external file fname. extracts next character from the input stream fsIn and places it in the character variable c. tests for the end-of-file condition. disconnects the stream and associated file. //Behaves just like cin

fsIn.get(char& c)
s

q q q

fsIn.eof()
s

fsIn.close()
s

fsIn >> c;

MP102 Prog. Fundamentals File I/O / Slide 26

Summary of Output File-Related Functions

#include <fstream> ofstream fsOut;


q q q q q

fsOut.open(const char[] fname)


s

connects stream fsOut to the external file fname. inserts character c to the output stream fsOut. tests for the end-of-file condition. disconnects the stream and associated file. //Behaves just like cout

fsOut.put(char c)
s

fsOut.eof()
s

fsOut.close()
s

fsOut << c;

Вам также может понравиться