Lab #9: File Processing – Direct Access

60-141
Intro to Algorithms & Programming II Winter 2015
Lab #9: File Processing – Direct Access
(Due at the end of the lab period or beginning of the next)
Objective: In this Lab you will practice using direct access writing and reading to and from a text
(character based) and binary (machine representation of data) file. In general, such files are called
binary files.
Background:.
To process direct access binary files in C, we use the header file <stdio.h> where various functions for
file processing are defined. A few of them are:
1. fopen (): to open a file,
2. fclose (): to close a file,
3. fread (): to read from a file, and
4. fwrite (): to write into a file.
5. fseek (): position the read-write head to a desired location for I/O
Work to do:
PART – A
Writing into a Direct Access File:
In this lab you will be using the structure (as defined below) and implement an interactive program
capable of prompting the user and storing 3 employee records into a file called "employee.dat". Write
a program called “Lab9a.c” that creates the file called “employee.dat”. This file will also be used in
the second part of this laboratory.
The format of the file would look like this sample (excluding the first line):
ID FIRSTNAME LASTNAME GPA
10 John Doe 64.5
20 Mary Jane 92.3
40 Alice Bower 54.0
30 Jim Smith 78.2
Instructions: Write a C program called "Lab9a.c" to accomplish the following: (The skeleton code is
given below for your convenience. Use the 3 functions specified to perform the input, printing and
saving the records to the file.)
#include <stdio.h>
struct employee {
char firstname[40];
char lastname[40];
int id;
float GPA;
};
typedef struct employee Employee;
/* Input the employee data interactively from the keyboard */
void InputEmpRecord(Employee *EmpList);
/* Display the contents of Employee records from the list */
void PrintEmpList(const Employee *EmpList);
/* Save the employee records from the list to the newly created
binary file specified by FileName */
void SaveEmpList(const Employee *EmpList, const char *FileName);
int main() {
Employee EmpList[4];
InputEmpRecord(EmpList);
PrintEmpList(EmpList);
SaveEmpList(EmpList, "employee.dat");
return 0;
}
Note that the function SaveEmpList() must perform opening of the file “employee.dat” for writing in
binary mode, then writing all records, and finally closing the file. Make sure that the character arrays
used for first and last name are fully initialized (e.g. use ‘\0’) before writing to file (to ensure that all
data fields hold initial data).
PART – B
Reading and modifying data from a Direct Access File:
Write a different C program called "Lab9b.c" that performs an in-place sort by GPA of the data in
“employee.dat”. This means that the records in the file must be re-arranged, or sorted, using the GPA
as the sorting key value. You may not use an array to hold all file data. You may only store records in
at most two data structures within your program. The records must be in order from highest to lowest
GPA. To prove if your program works, start by reading in each record and then outputting the
information to the monitor (stdout) until encountering end-of-file; during this process you can
determine the number of records in the file. After closing and re-opening the file, perform a sort in
much the same way that one sorts elements of an array – if two adjacent records are not in the correct
order, then swap them. Use a temporary structure to swap records. You may use any algorithm for
sorting. Finally, after the sort is finished, once again output all records to show that they appear in
sorted order.
Note: Students should examine the content of the file “employee.dat” to verify that not all information
in each record is human-readable as characters/digits/punctuation.
Using the data given above, the final sorted file output should look like:
ID FIRSTNAME LASTNAME GPA
20 Mary Jane 92.3
30 Jim Smith 78.2
10 John Doe 64.5
40 Alice Bower 54.0
EVALUATION:
You need to show your instructor the complete programs at the end of this lab, or at the beginning of
your next lab. The marks you will receive for this lab are made of two parts: Lab work marks 8 and
attendance marks 2. Total 10 marks.
Lab Work Mark: You will be evaluated based on your solutions for the problems based on the
following scheme:
0 mark = No work done.
2 mark = Incomplete code / does not compile, with no/invalid documentation
4 marks = Complete running program with no/invalid documentation
6 marks = Incomplete code / does not compile, with proper documentation
8 marks = Complete running program with proper documentation
IMPORTANT:
ASK QUESTIONS IF YOU GET STUCK, BUT DO YOUR OWN CODE. ANY CODE
SUSPECTED TO BE SIMILAR TO ANOTHER SUBMISSION WILL CAUSE BOTH
SUBMISSIONS TO RECEIVE A ZERO MARK ON ALL LABS AND BE REPORTED FOR
PLAGIARISM