/* A file named infile.txt contains a certain number (but not more than 128) of integers (see an example of such a file below). Write a complete C++ program to read in all of these integers and write them out neatly in reverse order to a file named outfile.txt. The input and output files must be opened in the main function. Your program must have the following 3 functions (not counting the main function): 1. a function to read in the integers from the file infile.txt, 2. a second function to reverse the order of the integers, 3. and a third function to write the result to the file outfile.txt in the form illustrated by the following example. For example, if the input file contains: 200 3 -8923 28 45 11 -34 then after the program has been run, the output file should contain: 0 -34 1 11 2 45 3 28 4 -8923 5 3 6 200 */ #include #include #include #include using namespace std; const int MAXSIZE(128); void readArray(ifstream& ins, int array[], int& size); void reverseArray(const int array[], int yarra[], int size); void writeArray(ofstream& outs, const int yarra[], int size); int main ( ) { int array[MAXSIZE], yarra[MAXSIZE], size(0); ifstream ins; ofstream outs; ins.open("infile.txt"); if (ins.fail()) { cout << "Failure in opening file infile.txt.\n"; exit(1); } outs.open("outfile.txt"); if (outs.fail()) { cout << "Failure in opening file outfile.txt.\n"; ins.close(); exit(1); } readArray(ins, array, size); reverseArray(array, yarra, size); writeArray(outs, yarra, size); ins.close(); outs.close(); return 0; } void readArray(ifstream& ins, int array[], int& size) { while(ins >> array[size]) size++; } void reverseArray(const int array[], int yarra[], int size) { for (int n = 0; n < size; n++) { yarra[n] = array[size-n-1]; } } void writeArray(ofstream& outs, const int yarra[], int size) { for (int n = 0; n < size; n++) { outs << setw(3) << n << setw(7) << yarra[n] << endl; } }