appending structure bin file
Can anyone please help me with an example of appending the end of a structure in a binary file. For instance, I have an array of 1000 structures, and for instance, so far the user has only wrote to the structure 300. I know hoe to open the file with fopen and fread, but what sort of code would I use to allow the user to write to the next available structure?
Code:
struct database {
char prod[60];
float cal;
float carb;
float prot;
float fat;
};
struct database mainm[1000];
Still problems, heres the code...
Ive tried but it will only write write to the structure which I specify, and if I dont specify a number I just get an error when compiling. This is what Im trying to do:
Code:
FILE *fp = fopen("output.dat", "ab");
printf("\nEnter the product:");
fgets(mainm.prod, 60, stdin);
if ((p = strchr(mainm.prod, '\n')) != NULL) //clears \n
*p = '\0';
system("cls");
printf("\nNow Please Enter The Nutritional Information:");
printf("\nEnter cal:");
scanf("%f", &mainm.cal);
fflushstdin();
printf("\nEnter carb:");
scanf("%f", &mainm.carb);
fflushstdin();
printf("\nEnter protein:");
scanf("%f", &mainm.prot);
fflushstdin();
printf("\nEnter fat:");
scanf("%f", &mainm.fat);
system("cls");
fflushstdin();
}