Homework 4 - Recursion and File I/O

Due: Monday May 9, 2011 at 5:00pm

The purpose of this assignment is to implement a recursive function that operates on a file of data and creates a file of output.

You will be creating a recursive function that reverses a string of characters from a file and creates a new file with the reversed text. The prototype for this function is:

void reverseFile(ifstream &, ofstream &);
The stopping condition for the recursion is reaching the end-of-file in the input file. When this happens, the function should return without calling itself recursively. This will trigger the backtracking behavior through the previous recursive function calls until one ends back up at main().

The recursive case for this function is to extract one character off the input stream using get() (you MUST use get() so spaces and newlines are also extracted from the file) and then call reverseFile(). AFTER reverseFile() returns, the character retrieved off the input stream is sent to the output stream. By doing the output after the recursive function call, the characters will be processed in reverse order.

The primary task in main is to set up the files for manipulation. You will have one input file which stores the data you are operating on and one output file which stores the result of the recursive function. Your main function will prompt the user for the name of the input file and read that in to a C-style string (character array) for the input filename. You will then create the output filename from the input filename following these steps:

copy the input filename over to the output filename array (strncpy)
append the literal string ".out" to the output filename array (strncat)
Make sure the output filename array is 4 characters longer than the input filename array so there is space to append ".out" to it.

You will then associate the input filename with the input file stream using the file stream open() function (likewise, associate the output filename with the output file stream). Once both file streams are succesfully opened, you can pass them in to the recursive function. When the recursive function returns, close both file streams.

You should be able to input any text file to the program and it should create the output file with all of the text reversed. Do keep in mind that the runtime stack does have a maximum stack depth, which will limit the number of characters you can process from the input file. However, the stack depth on Sleipnir is sufficient to process reasonably sized files such as a page or two of text (like this HTML file or even your source code file).