// FILE: copyfile.cc // COPIES FILE IN_DATA.DAT TO FILE OUT_DATA.DAT // BUG: Does not compile on SunOS Gnu CC at CSUSB // RJB Mon May 15 23:53:52 PDT 1995 #include // required for EXIT_FAILURE etc #include // required for external file streams // ASSOCIATE PROGRAM IDENTIFIERS WITH EXTERNAL FILE NAMES #define in_file "in_data.dat" #define out_file "out_data.dat" int main() { // Functions used ... // COPIES ONE LINE OF TEXT void copy_line (ifstream&, // IN: infile stream ofstream&); // OUT: outfile stream // Local data ... int line_count; // output: number of lines processed ifstream ins; // associates ins as an input stream ofstream outs; // associates outs as an output stream // Open input and output file, exit on any error. ins.open (in_file); // ins connects to file in_file if (ins.fail ()) { cerr << "*** ERROR: Cannot open " << in_file << " for input." << endl; return EXIT_FAILURE; } // end if outs.open (out_file); // outs connects to file out_file if (outs.fail ()) { cerr << "*** ERROR: Cannot open " << out_file << " for output." << endl; return EXIT_FAILURE; } // end if // Copy each character from in_data to out_data. line_count = 0; // initialize line_count to zero (0) copy_line (ins, outs); line_count++; while (!ins.eof ()) { copy_line (ins, outs); line_count++; } // end while // Display a message on the screen. cout << "Input file copied to output file." << endl; cout << line_count << " lines copied." << endl; ins.close(); // close input file stream outs.close(); // close output file stream return EXIT_SUCCESS; } // COPY ONE LINE OF TEXT FROM ONE FILE TO ANOTHER void copy_line (ifstream& ins, // IN: ins stream ofstream& outs) // OUT: outs stream // Pre: ins is opened for input and outs for output. // Post: Next line of ins is written to outs. // The last character processed from ins is ; // the last character written to outs is . { // Local data ... const char nwln = '\n'; // newline character char next_ch; // inout: character buffer // Copy all data characters from ins file stream to outs file stream. ins.get (next_ch); while ( (next_ch != nwln) && !ins.eof ()) { outs.put (next_ch); ins.get (next_ch); } // end while // If last character read was nwln write it to out_data. if (!ins.eof ()) outs.put (nwln); return; } // end copy_line