CS44 Lab 0: Binary I/O

Due by 11:59 p.m., Wednesday, January 28, 2015
`
Introduction

This assignment is to be done individually. Most subsequent lab assignments will be done with a partner.

The task for this week's lab is fairly manageable: given employee data from two separate files, combine the contents in a sorted list and output the results to file. You will implement a linked-list data structure to maintain your sorted list of employee data. This will simulate, at a low-level, a common operation for storing raw data in a DBMS.

While this lab serves primarily as a warm-up exercise and reminder of C++ programming, we will also introduce new concepts. The learning objectives for this assignment:

To get started, run update44 to obtain the starting point files in ~/cs44/labs/0/. You should find the following files (files highlighted in blue require modification):

When you are ready to submit your lab, use handin44. Recall that only files in the ~/cs44/labs/0 subdirectory will be submitted. You may submit as many times as you wish; only the most recent copy will be saved.

Implementation Details

EmployeeList class

In employee.cpp/h, you will implement an EmployeeList and related EmployeeNode class to manage a sorted linked list of employee data. The data will be sorted based on the name field. Each employee (stored as an EmployeeNode) will be described with a name (c-string) and salary (int). A list of more specific requirements and details:

Main program

In sortEmployees, you will then write a main program that reads in employee data from two unsorted binary files (the format of these files is described below), merges the data from two files together in sorted order on employee name, and outputs the resulting sorted list of employee data to a binary file.

Your program will also output information to standard output as it makes progress. Specifically, your program should use formatted output to display the employee data as seen in the input files and finalize with a display of the sorted list of employee data with average salary. as an example). The three files used by your program (two input and one output) will be passed to your program via command line arguments. See Tips and Hints for example usage.

Some more specifics to consider for your main program:


Tips and Hints

Format of Employee Files

Each employee file is a binary file that stores a sequence of variable length employee records. Each employee record has two fields: name (variable length) and salary (4 byte integer). The format of an employee record in the binary file is as follows:

  ----------------------------------------------------------------------------
 |    4 byte integer     |     N character string     |    4 byte integer     |
 | Number of characters  |        Employee name       |    Employee salary    |
 | in employee name (N)  |    (not null-terminated)   |                       |
  ----------------------------------------------------------------------------

Reading and Writing binary files

To read integers and character strings from a binary file you can use:

  //open file specified in filename. Second parameter specifies that
  // the file is for input and is in binary format
  fstream infile(filename, ios::in | ios::binary);
  int nameLen;
  char *name;

  // read 4 bytes into memory location of nameLen 
  // here we typecast the address of nameLen (the destination) as a char *
  infile.read((char*)&nameLen, sizeof(int));

  //Allocated space and read characters for string
  name = new char[nameLen+1];
  infile.read(name, nameLen);   
  // remember to null terminate strings with '\0'
  ...
  infile.close();

To write integers and character strings to a binary file you can use:

  fstream outfile(filename, ios::out | ios::binary);
  int nameLen = ...;
  char *name = ...;
  ...
  outfile.write((char*)&nameLen, sizeof(int));
  outfile.write(name, nameLen);
  outfile.close();

When you are done implementing the employee list classes, you should be able to type make and have your code compile to give an executable called sortEmployees. Run this executable to test your code.

Sample Output

# a run with the wrong number of command line args, should exit with message
$ ./sortEmployees 
usage: sortEmployees 'file1' 'file2' 'resultfile'

# a run with correct number of command line args:
$ ./sortEmployees input/infile1.dat input/infile2.dat result.dat
See here for the expected output.

The input data files are given to you with the starting point code. These files, however, do not test all corner cases for the linked list so be sure to design further tests. You should design a strategy for verifying your output files are correct, as well. For example, you can use createbin to create a binary file (given your expected result in text format) and then diff your actual result with the createbin result.


Submitting your lab

Submit using handin44. Please run make clean before submitting to keep file sizes down. Also, be sure to complete the README file.