Outputting the Sorted Data

At this point we have been passing around this flag which tells the program how to sort the data. But as yet we have not determined what method of sorting the user wants. This can be done in a couple of lines as follows-na:

int sortMethod;

printf("Do you want the data sorted by 0: HTTP code, or 1: IP address? ");

Again, if the program were more complex, we would invest in some checking to make sure the user had indeed entered either 0 or 1, however this will do for us. We can now pass sortMethod to our bubbleSort function as the flag, since the value we obtained from the user corresponds to the method of sorting that we want to use. Thus we simply add the following line to our main program after the data has all been read in:

bubbleSort(sortArray, numLines, sortMethod);

To output the sorted data, we will simply open a new file for writing and loop through the array (which is now sorted with the new order) and write the weblog lines to this new file in the new order that they lie in. This is straightforwardly accomplished as follows-na:

FILE * outFile;

outFile = fopen("sorted.out", "w");
for (int i = 0; i < numLines; i++)

The only thing that remains to be done in our program is to close the files we have open and quit. Closing the files writes any remaining (buffered) data to the file and closes the stream that we associated with the file by opening it. We close the files as follows-na:


Our program is now complete. A full version of the source code for this program can be downloaded here.