//-------------------------------------------------------------------- // // Example showing the use of an output file outfile.cpp // //-------------------------------------------------------------------- // Reads ALL the integer values from a user-specified input file and // outputs them to a user-specified output file. #include // For cin and cout #include // For file input/output void main() { ifstream inFile; // Input file stream ofstream outFile; // Output file stream char inFilename[21], // Input file name outFilename[21]; // Output file name int num; // Number read from file // Get the file names. cout << "Enter the name of the input file (try values.dat): "; cin >> inFilename; cout << "Enter the name of the output file: "; cin >> outFilename; // Open the input and output files. Flag ios::noreplace // indicates that the open operation should fail if the // file already exists. inFile.open(inFilename,ios::nocreate); outFile.open(outFilename,ios::noreplace); // Check that files opened correctly. if ( !inFile ) cout << "File " << inFilename << " could not be opened for input" << endl; else if ( !outFile ) cout << "File " << outFilename << " could not be opened for output" << " (Does it already exist?)" << endl; else { // Read in numbers and output them to the output file. while ( inFile >> num ) outFile << num << endl; // Close the input and output file streams. inFile.close(); outFile.close(); } }