Be sure you have read, or at least skimmed, the assigned readings for classes through 10/11.
Please include with each part of the assignment the Honor Code pledge or just the word ``pledged'', plus one or more of the following about collaboration and help (as many as apply).1Text in italics is explanatory or something for you to fill in. For written assignments, it should go right after your name and the assignment number; for programming assignments, it should go in comments at the start of your program(s).
Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to email@example.com with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 1120 hw 6'' or ``LL hw 6''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.
The program should take the name of the file to sort as a command-line argument (and print appropriate error messages if none is given or the one given cannot be opened) and write the result of the sort to standard output.
To do this, I think you will need to read the whole file into memory. There are various ways to do this and perform the sort; the one I want you to use is somewhat involved but intended to give you more practice working with pointers. To get full credit you must use the approach described here.
char * data = malloc(size_in_bytes);
You can now operate on data as if it had been declared as an array of char. (Check first that malloc succeeded.)
Now you can read in the contents of the file; a character at a time is probably simplest. Notice that as you do this you will get the newline characters at the ends of lines. (You might write this much of the program and check that it works before going on.)
The idea will be to build an array of pointers pointing to starts of lines, sort the pointers so the first one points to the first line to print, etc., and use them to print the lines in order. (If you think you know at this point how to proceed, you could try doing so, and then come back and read the rest of this description.)
So the next step is to
build the array of pointers to lines. How many do
you need? Well, you could figure that out as you're reading the
file into memory. Say you have that in a variable called N.
Then you can allocate space for an array of pointers like this:
char ** lines = malloc(sizeof(lines) * N);
The first one should point to data, which you can accomplish like this:
lines = &data;
Then the idea is to go through the rest of the characters, and make lines point to the character after the first newline, lines point to the character after the second newline, etc.
(You'll need this code anyway, so might as well write it now and check that it works. You may get a surprise when you first run it, as a result of which you may decide you need to do more processing of your data array. More-explicit hint in a footnote2 so you can at least try to figure it out for yourself first.)
You can check your program's output by using the sort command to sort the input file and comparing its result (captured with I/O redirection!) with your result (also captured with I/O redirection). Correction: Depending on your account's configuration, the sort may do a simple sorting based on what strcmp returns for the lines in the file, OR it may do a comparison that is case-insensitive and ignores leading whitespace. For this problem I just want you to do the simple comparison. You can get the sort command to do that by overriding the normal configuration, thus:
LC_COLLATE=C sort filetosort