http://www.oocities.org/SiliconValley/Peaks/8778/TAU_advprog.html

Advanced Programming Course - Exercise 2



To write a program that reads one text file and prints its contents into another file in the reverse order (meaning that lines are in the reverse order). I.e. if the input file contains:

aaa
The second line
A quick brown fox jumps over the lazy dog
The 4th line
etc. etc.
the last line

then the output file must contain:

the last line
etc. etc.
The 4th line
A quick brown fox jumps over the lazy dog
The second line
aaa

The two file names are given as the first two arguments to the program. If there is no arguments given - print an error message like "Program needs file name argument". If there is only one argument - treat it as the input file name and print the results to the standard output stream instead of physical file. When opening files check for the success, otherwise print a message like "Cannot open file .... for reading".

Remember that you know neither number of lines in the input file nor length of each line (although you can assume that each line is not greater than 1024 characters). So store the lines read in a list. Use for the list node:

typedef struct node_struct
{
	char*			line;
	struct node_struct*	next;
} NODE;

Use value NULL for the next field as a sign of the end of the list. At the end of the program the memory allocated for the list should be freed.