CSCI 235 — C structures, parsing, sorting, random I/O

Getting ready

Create a directory csci235/clab1 and store copies of the following files in that directory.

Let’s Take a moment while you try out the first three options listed on the very long gcc man page. Notice what each option produces.

Next take a look at the Makefile. It really can be that simple. However, just to make it a little more challenging, modify Makefile so that it will store the executable in csort rather than labstuff. I think this involves changing only one line.

Task 1 — reading and printing

Take a look at your files and figure out how to get your program to read and neatly print the information in course.txt. Hint: Look for the four strings than need filling in.

This does involve a serious look at fscanf() and fprintf(). Your program’s output must be displayed in neat lines.

Also, pay attention to that struct courseInfo definition. Remember, a struct is like a class with no methods.

Task 2 — sorting with void *

Take a look at the qsort() routine. Then look at the Microsoft Developer Network (MDN) example (and any other examples you can find) because the official description won’t be that useful.

Start the following incomplete routine.

int cmpLimit(const void *a, const void *b) {
  struct courseInfo *c1 = (struct courseInfo *)a ;
  struct courseInfo *c2 = (struct courseInfo *)b ;
  return your magic ;

Now add a line to call qsort to sort the array by course limit size.

Be sure to print the array after sorting so we can be sure it really has been sorted.

Task 3 — a better sort

Improve cmpLimit so that it breaks ties by department name, course number, and finally section number. Students who have taken CSCI 333 may have a slight advantage here.

Use strncmp() to comparse the department names. This one is a quite messy. My cmpLimit() is about a dozen lines.

Task 4 — Raw bits

Modify your program so that it writes a binary file. You really can’t look at a binary text file in a normal editor, but there are a surprizingly large number of hex editors for Linux. Look up the page Best Linux Hex Editors to see a listing. If we have time, you can try one out at the end of the lab. (A former CSCI 202 instructor required his students to learn how use a hex editor.)

Start by adding some code to allow the “user” of your program to specify a name for the binary output file on the command line. (Hint: Use argv[2] as the first argument to fopen.) Be sure to check the return value of your new fopen(): It better not be NULL. Also, expert C programmers call strerror() to generate a nice error message.

You also need to call fwrite to populate your binary file. It’s actually easier than using fprintf.

If we have time, look at your binary file using your favorite editor or just more it. Binary files are not made for human reading. I often just use od. Read Linux OD command Examples page. (Hint: -c and -x.)

The output file produced by your program should be 340 bytes long and have a checksum (as computed by cksum) of 350993223.

Task 5 — Direct I/O for binary

Finally, we are going to write a program that reads a record from the binary file. This program will be called, from the shell, as follows:

readrec binaryFileName recordNumber

This program isn’t that long. However, it needs to call the following routines to read the record.

I used scanf() to parse the record number. If you are really serious about error checking, try strtol().

You’ll probably need to follow along in class for this one.