// Lab #8: File It Away // Program: READINT2.CPP // This program reads all of the integer values in // a user-specified file, squares them, and writes // them back to a new file using exactly the same // format. #include #include #include void read_numbers (ifstream &f); void square_numbers (ifstream &f, ofstream &g); // The main program initializes the input and output // stream objects, first creating them and then // associating them with real file names. It calls // the square_numbers function to create a new file // with all of the values squared, and then calls // ReadNumbers twice to check both files by displaying // them on the screen. void main() { char infilename[20], outfilename[20]; ifstream fin, fin2; ofstream fout; // Create the input file stream fin cout << "Please enter a file name for input: "; cin >> infilename; fin.open(infilename); while (fin.fail()) { cout << "Cannot open " << infilename << " for reading." << endl; cout << "Please re-enter the file name: "; cin >> infilename; fin.open(infilename); } cout << endl; // Create the output file stream fout // You need to add this code. Use the string variable // "outfilename" to hold the file name and the stream // object fout, as they are necessary to the following code. // Square the numbers in the input file stream square_numbers(fin, fout); // Close both original files fin.close(); fout.close(); // Re-open both files for reading. fin.open(infilename); // NOTE that the same file can be both written and read // in the same program! fin2.open(outfilename); // Display the contents of the original file cout << endl << endl; read_numbers(fin); // Display the contents of the new file cout << endl << endl; read_numbers(fin2); // Close both files fin.close(); fin2.close(); } // Place your read_numbers function from the last // activity here. void read_numbers (ifstream &f) { } // This function should read the values from input // file f, square them, and write them to output // file g using the same format. void square_numbers (ifstream &f, ofstream &g) { }