#include <stdio.h>
struct invent_record
{
char name[10];
int number;
float price;
int quantity;
};
main()
{
struct invent_record item;
char filename[10];
int response;
FILE *fp;
long n;
void append (struct invent_record 8x, file *y);
printf("Type filename:");
scanf("%s", filename);
fp = fopen(filename, "a+");
do
{
append(&item, fp);
printf("\nItem %s appended.\n",item.name);
printf("\nDo you want to add another item\
(1 for YES /0 for NO)?");
scanf("%d", &response);
} while (response == 1);
n = ftell(fp); /* Position of last character */
fclose(fp);
fp = fopen(filename, "r");
while(ftell(fp) < n)
{
fscanf(fp,"%s %d %f %d",
item.name, &item.number, &item.price, &item.quantity);
fprintf(stdout,"%-8s %7d %8.2f %8d\n",
item.name, item.number, item.price, item.quantity);
}
fclose(fp);
}
void append(struct invent_record *product, File *ptr)
{
printf("Item name:");
scanf("%s", product->name);
printf("Item number:");
scanf("%d", &product->number);
printf("Item price:");
scanf("%f", &product->price);
printf("Quantity:");
scanf("%d", &product->quantity);
fprintf(ptr, "%s %d %.2f %d",
product->name,
product->number,
product->price,
product->quantity);
}
Output
Type filename:INVENTORY
Item name:XXX
Item number:444
Item price:40.50
Quantity:34
Item XXX appended.
Do you want to add another item(1 for YES /0 for NO)?1
Item name:YYY
Item number:555
Item price:50.50
Quantity:45
Item YYY appended.
Do you want to add another item(1 for YES /0 for NO)?0
AAA-1 111 17.50 115
BBB-2 125 36.00 75
C-3 247 31.75 104
XXX 444 40.50 34
YYY 555 50.50 45