Top Banner
Programming File I/O
20

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

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.

Programming

File I/O

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

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

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

COMP102 Prog. Fundamentals File I/O / Slide 3

Using Input/Output Files A computer file

is stored on a secondary storage device (e.g., disk);

is permanent; can be used to provide input data to a

program or receive output data from a program, or both;

must reside in Project directory (not necessarily the same directory as the .cpp files)

must be opened before it is used.

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

COMP102 Prog. Fundamentals File I/O / Slide 4

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).

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

COMP102 Prog. Fundamentals File I/O / Slide 5 Copyright © 2000 by Brooks/Cole Publishing Company

A division of International Thomson Publishing Inc.

C++ streams

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

COMP102 Prog. Fundamentals File I/O / Slide 6

Input File-Related Functions

#include <fstream>

ifstream fsIn; fsIn.open("fname.txt")

connects stream fsIn to the external file "fname.txt ".

fsIn.close() disconnects the stream and associated file.

fsIn >> c; //Behaves just like cin

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

COMP102 Prog. Fundamentals File I/O / Slide 7

Output File-Related Functions

#include <fstream>

ofstream fsOut; fsOut.open("fname.txt")

connects stream fsOut to the external file "fname.txt".

fsOut.close() disconnects the stream and associated file.

fsOut << c; //Behaves just like cout

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

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 <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std;int main(){

// declare the input and output streamsifstream input_stream;ofstream output_stream;

// open the input fileinput_stream.open("numbers.dat");// open the output fileoutput_stream.open("sum.dat");

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

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 fileoutput_stream << "The sum of the first 3\n"

<< "numbers in input_file.dat\n" << "is " << (first + second + third) << endl;

// close the input and output filesinput_stream.close();output_stream.close();return 0;

}

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

COMP102 Prog. Fundamentals File I/O / Slide 10

File I/O: Example 1.2

// copy everything from input.txt to output.txt#include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std;int main(){

// declare the input and output streamsifstream input_stream;ofstream output_stream;

// open the input fileinput_stream.open(“input.txt");// open the output fileoutput_stream.open(“output.txt");

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

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;

}

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

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 <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std;int main(){

// declare the input and output streamsifstream input_stream;ofstream output_stream;

// open the input fileinput_stream.open("numbers.txt");// open the output fileoutput_stream.open("sum.dat");

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

// 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 fileoutput_stream << "The sum is " << sum << endl;output_stream << "The average is “

<< double(sum)/counter << endl;

// close the input and output filesinput_stream.close();output_stream.close();return 0;

}

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

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 <iostream>#include <fstream>using namespace std;int main(){

ifstream ins;ofstream outs;int count=0;char next;

ins.open("indata.dat"); // open the input fileouts.open("outdata.dat"); // open the output file

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

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

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

Example 3

indata.dat:a b ctop10 methods to count spaces

1 3(eof)

Output to screen:a b cBlanks: 2top10 methods to count spacesBlanks: 4

Blanks: 01 3Blanks: 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.

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

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

int main(){ifstream ins;int count;char next;

ins.open("indata1.txt"); // open the fileins.get(next); // get the first char

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

COMP102 Prog. Fundamentals File I/O / Slide 18

File I/O: Example 3while(!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;}

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

COMP102 Prog. Fundamentals File I/O / Slide 19

Summary of InputFile-Related Functions

#include <fstream>ifstream fsIn; fsIn.open(const char[] fname)

connects stream fsIn to the external file fname.

fsIn.get(char& c) extracts next character from the input stream fsIn and places it

in the character variable c.

fsIn.eof() tests for the end-of-file condition.

fsIn.close() disconnects the stream and associated file.

fsIn >> c; //Behaves just like cin

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

COMP102 Prog. Fundamentals File I/O / Slide 20

Summary of OutputFile-Related Functions

#include <fstream>ofstream fsOut; fsOut.open(const char[] fname)

connects stream fsOut to the external file fname.

fsOut.put(char c) inserts character c to the output stream fsOut.

fsOut.eof() tests for the end-of-file condition.

fsOut.close() disconnects the stream and associated file.

fsOut << c; //Behaves just like cout