// this program reads pairs of numbers from input file 'numin.dat' // for each pair of numbers, it adds the numbers and writes the sum // to the output file 'numout.dat' #include <iostream.h> #include <fstream.h> void getInput(ifstream&,int&,int&); void write(ofstream&,int); void intro(ofstream&); int main() { int num1,num2; int sum; // create file objects and open them ifstream infile; ofstream outfile; infile.open("numin.dat"); outfile.open("numout.dat"); // write heading to output file intro(outfile); // read two numbers at a time, sum them, and output them // continue till end of file getInput(infile,num1,num2); while (infile) { sum = num1 + num2; write(outfile,sum); getInput(infile,num1,num2); } return 0; } // this function writes a heading to the output file void intro(ofstream& out) { out << "Program to demonstrate using files with functions\n\n"; } // this function reads two numbers from the input file void getInput(ifstream& in,int& num1,int& num2) { in >> num1 >> num2; } // this function writes the sum to the output file void write(ofstream& out,int sum) { out << sum << endl; }
10 20 50 50 25 35 3 6
Program to demonstrate using files with functions 30 100 60 9
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2004