CIS 111 Create a binary file

Objectives

  • Create an input loop to enter data
  • Stop input when the user chooses to end it
  • Validate user input
  • Write data to a random access binary file one record at a time
  • Write a flowchart that rerepsents the algorithm

Overview

This assignment involves the creation of a flowchart and a program that will create a binary random access, fixed-length record data file from user input. You will create the user interface and store the data the user enters. Each record in the file must have the following format:

  • department: String (3 characters)
  • employee id: String (5 characters)
  • month: int (must be between 1 and 12)
  • day: int (must be between 1 and 31)
  • year: int (must be between 2000 and 2100)
  • hours: double (must be between 0.0 and 24.0)

Requirements

Create a flowchart that represents the algorithm you will use.

Create an interface for the user that will loop until the user chooses to stop entering data. Each time the user completes the data entry for a record, append it to the end of a file named rafile.dat. Remember to delete the file if you decide to start entering data fresh. You must follow the sample input and output formats exactly and include error checking. The error messages can have a different format/text as long as they are clear. All input should be converted to uppercase internally. You can not easily check the contents of the output file just by opening it with a text editor because it is binary data. You can however check it by running a special program presented later on this page.

Submit the flowchart and the Python source code.

Sample Run

# rafile.dat erased # Python solution program run Enter dept ID: IT Employee ID: D4772 Enter month: 1 Enter day: 2 Enter year: 2011 Enter hours: 8.25 Enter another record? (Y/N) y Enter dept ID: IT Employee ID: F9642 Enter month: 1 Enter day: 4 Enter year: 2011 Enter hours: 8.25 Enter another record? (Y/N) n File rafile.dat has been modified. # Python solution program run Enter dept ID: IT Employee ID: V1001 Enter month: -9 Integer value must be between 1 and 12, please re-enter: 1 Enter day: 34 Integer value must be between 1 and 31, please re-enter: 3 Enter year: 08 Integer value must be between 2000 and 2100, please re-enter: 2011 Enter hours: 480 Double value must be between 0.0 and 24.0, please re-enter: 5.25 Enter another record? (Y/N) p Error: Character entered is not a valid response. Enter another record? (Y/N) Y Enter dept ID: MGT Employee ID: A0010 Enter month: 1 Enter day: 2 Enter year: 2011 Enter hours: 6.5 Enter another record? (Y/N) N File rafile.dat has been modified.

Checking your results

A binary file can not be checked by opening it up in a text editor. You need a program that knows what fields to expect. The following Python program will allow you to check the file your program has produced (rafile.dat) to see what the contents are:

import os import struct filename = "rafile.dat" recordSize = 28 fileSize = os.path.getsize(filename) if fileSize % recordSize != 0: print("Error: Corrupted file") else: numberRecords = fileSize // recordSize with open(filename, mode="rb") as file: for recordNumber in range(1, numberRecords + 1): fileContent = file.read(recordSize) tpl = struct.unpack(">3s5siiid", fileContent[:28]) print("%3d: %s" % (recordNumber, str(tpl)))

You should see output that looks something like this:

1: (b'IT ', b'D4772', 1, 2, 2011, 8.25) 2: (b'IT ', b'F9642', 1, 4, 2011, 8.25) 3: (b'IT ', b'V1001', 1, 3, 2011, 5.25) 4: (b'MGT', b'A0010', 1, 2, 2011, 6.5)

Rubric

  • 12 points for creating a correct flowchart
  • 5 points for having the input loop run and end properly
  • 2 points for converting all text input to uppercase
  • 9 points for correctly reading and validating the required input
  • 6 points for writing the data correctly to a random access file
  • 3 points for appending data to the file rather than overwriting existing records
  • 3 points for proper documentation comments and for correctly following coding conventions (indentation, naming rules, NO tabs, etc.)