Program 2 Introduction to C CSCI 112, Spring 2018 Description A grocery store wa
ID: 3705097 • Letter: P
Question
Program 2 Introduction to C CSCI 112, Spring 2018 Description A grocery store wants to keep a database of the items they sell. You will write 2 programs. One program will read in (from a text file) the current items they sell and save those items to a binary file. The second program will read in the binary file and provide a user interface to allow for adding items, providing the number of items ina particular category and for listing the contents of the file by category to the screen. The information for each item (no more than 100 items) will be name of item (no more than 20 characters), the cost of the item in cents, aisle number and a character representing the product category (M for meats, P for produce, D for dairy, and C for canned foods). The following is an example line from the text file Corn 89 3 C Required You must store each item in a structure There must be only 1 binary file Your input will be the file grocery.txt in /home/public. 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): (lf 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:1Explanation / Answer
Program 1
Read text file and write the content into binary file
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *ptr,*ptr1;
char buff[1000];
ptr=fopen("D://C Program/grocery.txt","r"); //text file open in read mode
ptr1=fopen("D://C Program/mybin.bin","wb"); // binary file open in write mode
if(ptr==NULL)
{
puts("File could not open");
}
while(fscanf(ptr, "%s", buff)!=EOF)
{
printf("%s",buff);
fwrite(buff, sizeof (buff), 1, ptr1); //writing into binary file pointer
}
fclose(ptr);
fclose (ptr1);
}
Question no.2 Input based operation on file
#include<stdio.h>
#include<conio.h>
struct record
{
char name[10];
int cost;
int aisle;
char cate;
};
int main()
{
FILE *infile;
struct record input;
puts("Enter choice A.Add Item B.List all items");
char choice=getchar();
if(choice=='A')
{
infile = fopen ("D://C Program/tom.txt", "w");
if (infile == NULL)
{
fprintf(stderr, " Error opening file ");
}
printf("enter name");
scanf("%s",input.name);
printf("enter cost");
scanf("%d",input.cost);
printf("enter aisle");
scanf("%d",input.aisle);
printf("enter category");
scanf("%c",input.cate);
fwrite(&input, sizeof(struct record), 1, infile);
}
if(choice=='B')
{
infile = fopen ("D://C Program/tom.txt", "r");
if (infile == NULL)
{
fprintf(stderr, " Error opening file ");
}
// read file contents till end of file
while(fread(&input, sizeof(struct record), 1, infile))
printf ("name = %s cost = %d aisle=%d category=%c",input.name,input.cost,input.aisle,input.cate);
}
fclose(infile);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.