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

Getting ready

Store copies of the following files:

Task 1 — reading and printing

Take a look at your files and figure out how to get labstuff to process course.txt. Mostly, you have to fill in some silly strings.

This does involve a serious look at fscanf().

Be sure to pay attention to that struct courseInfo definition.

Task 2 — sorting with void *

Take a look at the qsort() routine. Then look at the Microsoft example (and any other examples you can find).

Add 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.

Task 3 — a better sort

Improve cmpLimit so that it breaks ties with department name, course number, and finally section number.

Students who have taken CSCI 333 may have an advantage here.

Task 4 — Raw bits

Modify your program so that it writes binary output. You will need to add some code to use argv[2] as the name of an output file. Be sure to check the return code of your new fopen call which should look like this.

  FILE *outFile = fopen(argc[2], "w") ;

Calling fwrite to populate your binary file. It’s actually easier than using fprintf.

Look at your binary file using your favorite editor or just more it. Binary files are not made for human reading. Better yet, use the od. Read Linux OD command Examples page. (Hint: -c and -x.)

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:

labstuff binaryFileName recordNumber

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