// AddLinesOfInt.cpp // Add Up values of each integeron a line in an ifstream // and output sum to an ofstream. Last line out is total // of all ints in input file #include #include #include #include "utility.h" void AddLinesOfInt(ifstream &,ofstream &); void main() {ifstream Source; ofstream Dest; if (OpenForRead(Source)<0) { cout << "Open Failed..Check the Data File Name\n"; exit(1); } OutputDirection(Dest); AddLinesOfInt(Source,Dest); Source.close(); Dest.close(); } // Perform the Conversion // Precondition: Both arguments represent files successfully opened void AddLinesOfInt(ifstream &Infile,ofstream &Outfile) {char Line[81]; // Why 81, if we're only reading 80 below? char *Token; int Sum,Total=0; while (!Infile.eof()) { Sum=0; Infile.getline(Line,80,'\n'); Token=strtok(Line," \t\n"); if (Token) { Sum+=atoi(Token); while (Token=strtok(NULL," \t\n")) Sum+=atoi(Token); } Total+=Sum; Outfile << Sum << endl; } Outfile << Total << endl; }