Intermediate C - Homework #1

 

Your assignment is to create a program that loads an entire text file into memory. The program will utilize and demonstrate dynamic memory allocation and file access. Since this is still a review assignment, don't worry about doing it right or wrong. Relax. Simply try your best. The purpose of this assignment is to judge our knowledge as a group, so that I can address whatever issues are needed.

See the Functions section below for a list of suggested functions to be written. Consider each function a 'stage' of development of the assignment. Compile and test during each stage.

File Processing

Reading the file

You will be reading the file twice: once to get the statistics of the file (the number of lines, and the length of the longest line). This is an inefficiency that will go away in later versions of the program.

 

Memory Allocation

Your program must store then entire text of a file in memory, without advanced knowledge of the format of the file (i.e., the number of lines, or the length of the longest line). In order to store the file dynamically in memory, you will need the following primary allocations:

An array of char pointers

An array of char*'s must be allocated, with one extra pointer, to store 1 line of the file in each element. Since the number of lines depends on the text file to be processed, the size of this array is known. You will need to dynamically allocate the array. Consider the following code:

char ** Lines;

Lines = calloc( LineCount+1, sizeof(char*) );

The above code assumes that LineCount already contains the number of lines, determine by an initial processing of the file.

An Input Buffer

An input buffer should be created, to read in each line from the file, one at a time. Like the number of lines, you must dynamically determine the length of the longest line in the text file, so that the buffer you create dynamically is large enough to hold it. Consider the following code:

char * Buffer;

Buffer = malloc( LongestLine+1 );

The code above assumes that LongestLine already contains the length of the longest line.

Individual Lines of Text

You will use Buffer to temporarily store the contents of one line of text from the file. You are to take that buffer, and dynamically allocate a duplicate of it, and store it into the Lines array previously allocated. Consider the following:

Lines[CurLine++] = strdup( Buffer );

This step will need to be repeated for each line of text read from the file.

 

Functions

It is recommended you write the following functions. The names of the functions are indicative of what they do. Your goal in writting these routines is to provide a very simple and easy-to-read main function to actually accomplish your task:


unsigned int ReadInLines( char *Filename, char ***Lines );

Reads Filename into memory, and sets Lines to point to that memory. Returns an error value that indicates the error, and returns zerop upon success. An example of calling this function would be:

char **Lines;

ReadInLines( "c:\\autoexec.bat", &Lines );


void GetFileStats( FILE * File, int *LineCount, int *LongestLine );

Reads data from File, counting the Carriage Returns to determine the LineCount, and the number of characters between Carriage Returns to determine the LongestLine. Called by ReadInLines.


void PrintLines( char **Lines );

Lists the lines of text from Lines to the screen.


void FreeLines( char **Lines );

Releases memory that Lines refers to.


void SortLines( char **Lines );

Optional: Sorts Lines based on normal ascending sort (phone book order).