C++ random acesss files, structures Objectives :Create a loop with a menu of opt
ID: 3576557 • Letter: C
Question
C++ random acesss files, structures
Objectives:Create a loop with a menu of options, write to and read from a binary file, use a struct to store a record's data, figure out which C++ libraries are required
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
Requirements:Implement the following pseudocode:
Sample Run:
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
//#include<stdio.h>
using namespace std;
//structure defined
typedef struct Item
{
int id, qty;
double price;
}IT;
IT item;
//Accept data for the item
void getData()
{
cout << " Enter an ID: ";
cin >> item.id;
cout << "Enter a Price: ";
cin>>item.price;
cout << "Enter a Quantity: ";
cin >> item.qty;
}
//Counts and returns the number of records in the file
int recordCount()
{
fstream f;
//Opens the file in read mode
f.open("rafile.bin", ios::binary | ios::in);
//Counter is initialized to 0
int co = 0;
//Moves to the starting position
f.seekg(0L, ios::beg);
//Reads data from file
while(f.read((char *) &item, sizeof(item)))
//Updates the counter
co++;
//Close file
f.close();
//Return counter
return co;
}
//Appends record in file
void Append()
{
fstream f;
//Open file in append mode
f.open("rafile.bin", ios::binary | ios::app);
//Check for the file can be opened or not. if not show error message and exit
if(!f)
{
cout<<" Unable to open file";
exit(0);
}
//if it can be opened
else
{
//Accept data
getData();
//Write it to the file
f.write((char *) &item, sizeof(item));
}
//Close file
f.close();
//Display record counter status
cout<<" Record "<<recordCount()<<" written to file";
}
//Read specified record from the file and display it
void readRecord(int recno)
{
fstream f;
//Open the file in read mode
f.open("rafile.bin", ios::binary | ios::in);
//Set the counter to 1 and flag for availability of the record to 0
int co = 1, fla = 0;
//Move to the starting position
f.seekg(0L, ios::beg);
//Read record from file
while(f.read((char *) &item, sizeof(item)))
{
//Check for the specified record availability
if(co == recno)
{
//If available update the status to 1
fla = 1;
//Display the record
cout<<" "<<item.id<<": $"<<item.price<<" ("<<item.qty<<")";
//Come out of the loop
break;
}
//If not increase the record counter
else
co++;
}
//Check the flag status if it is zero then record not found displayed
if(fla == 0)
cout<<" Error: Invalid record number.";
//Close the file
f.close();
}
//Menu for user choice
void menu()
{
cout<<" r) To Read Record";
cout<<" a) To Append Record";
cout<<" q) To Quit Record";
}
//Driver function
int main()
{
int recordNo;
char ch;
//Initial status of the file
cout<<" There are "<<recordCount()<<" records in the file";
//Loops till 'q' or 'Q' is entered by the user
do
{
//Displays menu
menu();
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Checks for the user entered option
switch(ch)
{
//Displays the specified record
case 'r': case 'R':
//Checks if the file contains 0 record
if(recordCount() == 0)
cout<<" Record number to read: "<<recordCount();
else
{
//Accepts the record number to display
cout<<" Record number to read: ";
cin>>recordNo;
//Displays the specified recod
readRecord(recordNo);
}
break;
//Append record
case 'a': case 'A':
Append();
break;
//Exit
case 'q': case 'Q':
exit(0);
//Invalid choice
default:
cout<<" Invalid Choice. Try again";
}
}while(1);
}
Output:
There are 0 records in the file
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: r
Record number to read: 0
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: a
Enter an ID: 11
Enter a Price: 56.33
Enter a Quantity: 1
Record 1 written to file
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: a
Enter an ID: 22
Enter a Price: 78.45
Enter a Quantity: 2
Record 2 written to file
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: a
Enter an ID: 33
Enter a Price: 98.12
Enter a Quantity: 30
Record 3 written to file
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: r
Record number to read: 2
22: $78.45 (2)
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: r
Record number to read: 3
33: $98.12 (30)
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: r
Record number to read: 9
Error: Invalid record number.
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: t
Invalid Choice. Try again
r) To Read Record
a) To Append Record
q) To Quit Record
Enter your choice: q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.