// File: Makedb.c // Create a binary encoded file example // Each record has four fields, as described in the declaration of ItemRec below. #include #include #include #include typedef struct /* The Data Structure for one item of the inven- */ { int BarCode; /* tory. */ float UnitPrice; int Quantity; char Description[40]; } ItemRec; // This is a C-style struct declaration main() {ItemRec Product; FILE *fp; int Items,total,count; char S[20]; fp=fopen("DATA","w+b"); printf("How Many Items >"); scanf("%d",&Items); fwrite(&Items,sizeof(int),1,fp); total=0; // Add one record per iteration for (count=0;count <= (Items-1);count++) { // Input fields printf("BarCode (int)>"); scanf("%s",S); Product.BarCode=atoi(S); printf("Price (real)>"); scanf("%s",S); Product.UnitPrice=atof(S); printf("Quantity (int)>"); scanf("%s",S); Product.Quantity=atoi(S); printf("Description >"); scanf("%s",Product.Description); total+=Product.Quantity; // Write in binary form fwrite(&Product,sizeof(ItemRec),1,fp); } // Write total items at end of binary file fwrite(&total,sizeof(int),1,fp); fclose(fp); }