CIS 150 Programming Project: Random access (binary) file I/O

Objectives

  • Create a loop with a menu of options
  • Write to a binary file
  • Read from a binary file
  • Use a struct to store a record's data
  • Format output

Overview

This assignment involves the use of a struct and a random access binary file. You will create the user interface which lets the user store and retrieve records. Each record in the file has the following fields:

  • id: integer
  • price: double
  • quantity: integer

Program requirements

Implement the following pseudocode:

Define a struct that matches the description of the record above. main: Declare any needed local variables. Create a variable to store one record. // The complicated logic in this section takes care of the // case when the file does not initially exist. open the file "rafile.bin" for binary input and output if it did not open then // File did not exist so create it open the file "rafile.bin" for binary output if the file opened then // File exists now, so reopen for input and output close the file open the file "rafile.bin" for binary input and output otherwise // File still does not exist, so end the program display an error message (file could not be opened) exit with an error status find out how many records are in the file and store that in numRecs display the number of records that are in the file do the following display a menu as shown in the sample runs provided with this assignment get the user's choice and convert it to lowercase if the user chose to read a record then ask for a record number if that record number does not exist then display an error message otherwise read that record and display it (with 2 decimals for price) otherwise if the user chose to append a record then get an id, price, and quantity from the user and store it in a record write that record to the end of the file increase the numRecs variable display which record number was written to the file otherwise if the user chose to quit then do nothing otherwise display an error message for invalid option chosen while the user has not chosen to quit close the file exit with a success status

Sample Run

There are 0 records in the file r) read a record a) append a record q) quit Enter your choice: d Invalid choice. Try again. r) read a record a) append a record q) quit Enter your choice: r Record number to read: 0 Error: Invalid record number. r) read a record a) append a record q) quit Enter your choice: r Record number to read: 1 Error: Invalid record number. r) read a record a) append a record q) quit Enter your choice: a Enter an ID: 33 Enter a price: 13.1 Enter a quantity: 2 Record 1 written to file r) read a record a) append a record q) quit Enter your choice: 7778 Invalid choice. Try again. r) read a record a) append a record q) quit Enter your choice: Invalid choice. Try again. r) read a record a) append a record q) quit Enter your choice: Invalid choice. Try again. r) read a record a) append a record q) quit Enter your choice: Invalid choice. Try again. r) read a record a) append a record q) quit Enter your choice: a Enter an ID: 7778 Enter a price: 45.626 Enter a quantity: 1 Record 2 written to file r) read a record a) append a record q) quit Enter your choice: r Record number to read: 1 33: $13.10 (2) r) read a record a) append a record q) quit Enter your choice: r Record number to read: 2 7778: $45.63 (1) r) read a record a) append a record q) quit Enter your choice: q

Rubric

  • Note: Points may be deducted for not meeting the course coding standards.
  • 2 points for having the struct correct
  • 3 points for getting the file opened properly for reading and writing binary data
  • 3 points for correctly figuring out the number of records
  • 3 points for having a correct input loop that quits when it should
  • 1 points for converting the user menu choice to lowercase
  • 2 points for having a correctly functioning menu so each choice does the proper thing
  • 6 points for getting the read menu option functioning properly
  • 2 points for formatting the output of a record read as shown in the sample run
  • 8 points for getting the append menu option functioning properly