Question: Write a C (NOT C++) program will read in the binary file and provide a user interface to allow for adding items, providing the number

Write a C (NOT C++) program will read in the binary file and provide a user interface to allow for adding items, providing the number of items in a particular category and for listing the contents of the file by category to the screen.

The user interface (for the second program) will be: Select option (A-add item, N-number of items in category, L-list all items, X-exit pgmZZ): (If A is selected) Enter name of item to add: Enter price: Enter aisle: Enter category:

(If N is selected) Enter category to print: (And then print-) There are number of items in the Canned Goods category (If L is selected) (Print all items by category. For example? Canned Goods Corn, price: 89 cents, Aisle: 1 the item file and binary code are as follows:

grocery.txt

corn 89 3 C greenbeans 60 3 C groundbeef 430 17 M wholemilk 250 16 D cheese 150 16 D apples 50 1 P cabbage 100 1 P cherries 150 1 P cabbage 75 3 C hominy 45 3 C filetmignon 900 17 M chicken 450 17 M halfandhalf 250 16 D sourcream 150 16 D grapes 100 1 P corn 50 1 P watermelon 200 1 P peas 89 3 C porkchops 200 17 P creamcheese 150 16 D

#include #include #include //Define the text and binary file names #define TEXTFILE "Groceries.txt" #define BINFILE "Groceries.bin" //Declare the structure Grocery typedef struct { char name[20]; int cost; int aisle; char catagory; }Grocery; int main() { FILE *fptr, *bptr; // Open text file in readonly mode and binary file in write binary mode fptr = fopen(TEXTFILE, "r"); bptr = fopen(BINFILE, "wb"); // Keep a count of no. of items being read int itemsCnt = 0; // If any error in opening files, then return error if(fptr == NULL || bptr == NULL) { perror("Error in processing files"); return 0; } Grocery Item; // Read all items until end of file while(!feof(fptr)) { //Increment items count itemsCnt++; // Read items in the order in which they are present fscanf(fptr, "%s %d %d %c ", Item.name, &Item.cost, &Item.aisle, &Item.catagory); // Write the same into the binary file fwrite(&Item, sizeof(Grocery), 1, bptr); } // Close both text and binary files fclose(fptr); fclose(bptr); /* // Uncomment this part for reading the binary file what you have created //Open binary file in read binary mode bptr = fopen(BINFILE, "rb"); printf("Reading Binary File Items... "); while(itemsCnt--) { // Read count no. of items in the same order it was written fread(&Item, sizeof(Item), 1, bptr); // Print the item details printf("%s %d %d %c ", Item.name, Item.cost, Item.aisle, Item.catagory); } // Close the file after reading fclose(bptr); */ }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!