// Lab #8: File It Away // Program: READINT1.CPP // This program reads a specific number of rows of // integers with a specific number of values per line // from a user-specified file. #include #include #include void read_numbers (ifstream &f); void main() { char filename[20]; ifstream fin; // Declare and define the input file stream fin. cout << "Please enter a file name: "; cin >> filename; fin.open(filename); while (fin.fail()) { cerr << "Cannot open " << filename << " for reading." << endl; cout << "Please re-enter the file name: "; cin >> filename; fin.open(filename); } // Display the contents of fin to the screen. read_numbers(fin); // Close the file stream fin.close(); } // This function reads for a specific number of rows // and a specific number of values per row in the // given input file stream f. It then writes those // values to the screen in the presumed format. void read_numbers (ifstream &f) { int numlines, numvalues, number, i, j; cout << "Enter the number of lines in your file: "; cin >> numlines; cout << "Please enter the number of values per line. " << endl; cout << "(They must be the same for each line.): "; cin >> numvalues; cout << endl; cout << "The file contents: " << endl << endl; for (i = 1; i <= numlines; i++) { for (j = 1; j <= numvalues; j++) { f >> number; cout << setw(8) << number; } cout << endl; } }