#include <stdio.h>
main()
{
FILE *fp;
int number, quantity, i;
float price, value;
char item[10], filename[10];
printf("Input file name\n");
scanf("%s", filename);
fp = fopen(filename, "w");
printf("Input inventory data\n\n");
printf("Item name Number Price Quantity\n");
for(i = 1; i <= 3; i++)
{
fscanf(stdin, "%s %d %f %d",
item, &number, &price, &quantity);
fprintf(fp, "%s %d %.2f %d",
item, number, price, quantity);
}
fclose(fp);
fprintf(stdout, "\n\n");
fp = fopen(filename, "r");
printf("Item name Number Price Quantity Value\n");
for(i = 1; i <= 3; i++)
{
fscanf(fp, "%s %d %f d",item,&number,&price,&quantity);
value = price * quantity;
fprintf(stdout, "%-8s %7d %8.2f %8d %11.2f\n",
item, number, price, quantity, value);
}
fclose(fp);
}
Output
Input file name
INVENTORY
Input inventory data
Item name Number Price Quantity
AAA-1 111 17.50 115
BBB-2 125 36.00 75
C-3 247 31.75 104
Item name Number Price Quantity Value
AAA-1 111 17.50 115 2012.50
BBB-2 125 36.00 75 2700.00
C-3 247 31.75 104 3302.00