COMP1521 18s1 COMP1521 18s1 Final Exam Comp Sys Fundamentals
[Instructions] [Documentation]
[Q1] [Q2] [Q3] [Q4] [Q5] [Q6] [Q7] [Q8] [Q9] [Q10]

Question 3 (11 marks)

Student records are a very important part of a university's infrastructure. Maintaining student data in a form that can be efficiently accessed is critical. Consider the following file of Student records, ordered by student ID (for efficient lookup):

Each Student record contains (id, name, degree, wam); a student data file consists of a sequence of such records, ordered by student id. Since these records are stored in binary form, you cannot read student data files directly using the standard Unix text tools. A program stu is provided to display a file of student data.

In the q3 directory are six files and a testing directory:

Makefile

Organises the compilation of the stu and addstu programs

Students.h

A file defining the data type for Student records, and the function prototype for the addStudent() function.

stu.c

A C main program that ....

  • opens a file whose name is given as argv[1]
  • scans the student records in this file
  • displays the records, one-per-line on the standard output

addstu.c

A C main program that ....

  • opens a file whose name is given as argv[1]
  • collects information about a student from the other command-line arguments
  • populates a Student record using this information
  • invokes the addStudent() function to insert the record into the file

q3.c

A file containing a template for the addStudent() function.

studes

A data file containing student records, which you can view using the stu program (you will need to compile it first). You can also use this for initial testing, but make a copy before you run addstu on it. If your program corrupts it, it will no longer be usable for testing.

The tests directory contains data files (called stu?), test scripts, and expected outputs. If you want to test your program on these data files, make a copy first; otherwise the check tests, which use these data files, will fail.

Exercise

Your task is to implement the addStudent() function. This function takes two parameters:

The function scans the file, and finds the correct location in the file where the new Student record should be placed. It then adds it to the file, preserving all of the other records, and preserving the student id ordering.

Notes:

You can test your addStudent() function using a copy of the studes file in the current directory, e.g.

$ ./stu studes
5012345 Mary 3778 90.0
5013999 Dave 3707 75.2
5123456 Mary 3978 88.8
5298765 Arun 3707 74.3
5312345 Wei 3778 90.1
$ cp studes studes1
$ ./addstu studes1 5155555 "John Smith" 3529 40.0
$ ./stu studes1
5012345 Mary 3778 90.0
5013999 Dave 3707 75.2
5123456 Mary 3978 88.8
5155555 John Smith 3529 40.0  ... this will only appear once your function is correct
5298765 Arun 3707 74.3
5312345 Wei 3778 90.1

Once again, don't run tests on studes. Once you've corrupted it (e.g. by a buggy addStudent() function), it's no longer useful for testing. Always make a copy and do your testing on the copy.

Note that the tests used by check first make a copy of their data file before running the addstu program, so you can run them multiple times without breaking the data files in the tests directory.

You can test your q3 program with all of the data files in the tests directory using the command:

$ check q3

Once you are satisfied with your program, submit it using the command:

$ submit q3

This will make a copy of the q3.c file as your answer for this question. You can run the submit command as many times as you like. Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using inputs which may be different to the examples in the tests directory.

If, at some stage, you need to "re-install" the files (although you should not need to), you can copy all of the original files into the q3 directory by running the command:

$ re-start q3

Beware: this will overwrite all of your exsting files for this question, so only do it if you seriously mess things up.