Homework 1 -- Random I/O in C

Preparation

Within your Computer Science home directory create a protected subdirectory for this assignment by typing the following commands:

Now copy some initial files for the assignment into this directory by typing the following commands:

This should copy four files into your directory.

The big picture

You are going to write a program to search a range of 16-byte records stored within a file for a specific key. The records are pretty simple. They consist of a

A definition of an appropriate C data structure for the record is shown below and can be found in your file recform.h.

typedef struct simpRec {
   int      value ;
   char     name[12] ;
} simpRecT ;

The not quite-so-big picture

The "user interface" for this assignment is already defined. It is implemented by a main routine stored in the file home01.cpp that you copied into your directory. You should not need to modify this file to complete this assignment.

This main routine will open the file /usr/local/csci/343/testdata/home01.dat and prompt for

  1. a key, a character string up to twelve characters in length
  2. a starting record number
  3. an ending record number

The main routine then call a procedure rangeSearch that you must write. A stub routine for rangeSearch has also been copied into your directory as rsearch.cpp. You'll be glad to know that you have Makefile to help you compile all this code so that an executable is stored in home01.

Here's a sample run of a correctly running solution to homework 1.

woodfin% home01
Enter search key: CCCC
Enter indices of starting and ending records
Start (0 to 50624): 0
End (0 to 50624): 30000

Starting search
  Key:    CCCC
  Start:  0
  End:    30000
Record found with key CCCC and value 2020202

New key: BBBB
Enter indices of starting and ending records
Start (0 to 50624): 0
End (0 to 50624): 30

Starting search
  Key:    BBBB
  Start:  0
  End:    30
No record found with key BBBB

New key: BBBB
Enter indices of starting and ending records
Start (0 to 50624): 20000
End (20000 to 50624): 40000

Starting search
  Key:    BBBB
  Start:  20000
  End:    40000
No record found with key BBBB

New key: BBBB
Enter indices of starting and ending records
Start (0 to 50624): 3616
End (3616 to 50624): 3617

Starting search
  Key:    BBBB
  Start:  3616
  End:    3617
Record found with key BBBB and value 1010101

New key: BBBB
Enter indices of starting and ending records
Start (0 to 50624): 3616
End (3616 to 50624): 3616

Starting search
  Key:    BBBB
  Start:  3616
  End:    3616
Record found with key BBBB and value 1010101

New key: ^D
woodfin% 

If you really want more information about the testdata file, home01.dat, you might want to look at

Your part of the assignment

Your routine rsearch has the following C++ prototype:

The five arguments are

  1. the opened file,
  2. the up-to-twelve character key,
  3. the beginning position,
  4. the ending position,
  5. a buffer for storing the record

Your routine should return a non-zero value if the record is found, in which case the record should also be copied into the buffer, and a zero value otherwise.

This is not hard. Only fifteen lines of C++ code are required to complete the assignment with a solution with I/O error handling.

Due dates and turning it in

At noon on Wednesday, 17 February, a script will be run to copy rsearch.cpp from your csci/343/home01 directory. This script will be run at noon for five consecutive days. Each day your rsearch.cpp is missing or fails to successfully solve the problem, 20% will be deducted from your grade for homework one.


Back to the CSCI 363 homework page
Back to the CSCI 363 home page