Question: Hello! I just need help understanding binary files. I'm trying to save my inventory so that even when the program ends, the information still exists
Hello! I just need help understanding binary files. I'm trying to save my inventory so that even when the program ends, the information still exists and won't disappear.
I followed an example of binary files online to structure void saveAutos(), but I'm confident it's not completely right.
As always, any and all help is appreciated!!!!!!
Solution: #include
#define CLEAR system("cls") #define PAUSE system("pause") #define SIZE 100
typedef struct{ int year; char make[10]; char model[10]; char color[10]; char condition[10]; double price; }AUTO;
int getChoice(); void addAuto(AUTO inventory[], int *esize); void deleteAuto(AUTO inventory[], int *esize); void displayInventory(AUTO inventory[], int esize); void saveAutos();
int main(){ char choice; int esize = 0; AUTO inventory[SIZE];
do{ choice = getChoice(); switch(choice){ case 1: addAuto(inventory, &esize); break; case 2: deleteAuto(inventory, &esize); break; case 3: displayInventory(inventory, esize); break; case 4: printf("Exiting the program now, thank you for using my program. "); PAUSE; break; default: printf("Invalid selection, please select one of the options above. "); PAUSE; break; } } while (choice != 4); return 0; }
int getChoice(){ int results;
printf("Please make a selection: "); char menu[100]="1. Add a Auto 2. Delete Auto 3. Display Inventory 4. Exit "; printf("%s", menu); scanf("%i", &results);
return results;
} void addAuto(AUTO inventory[], int *esize){
if(*esize == SIZE){ printf("No additions allowed, inventory is full."); PAUSE; return; } printf(" Entering the data: "); printf(" Year: "); scanf("%d", &inventory[*esize].year); printf("Make: "); scanf("%s", inventory[*esize].make); printf("Model: "); scanf("%s", inventory[*esize].model); printf("Color: "); scanf("%s", inventory[*esize].color); printf("Condition: "); scanf("%s", inventory[*esize].condition); printf("Price: "); scanf("%lf", &inventory[*esize].price);
*esize = *esize + 1; }
void deleteAuto(AUTO inventory[], int *esize){ int index; int i, j; if(*esize == 0){ printf("No autos in inventory! "); return; }
printf("Please enter the index of the auto you want to delete: "); scanf("%d", &index); index--;
for(j = index; j < *esize-1; j++){ inventory[j] = inventory[j+1]; }
*esize = *esize-1;
printf("Auto deleted successfully! Return to main menu and select option 3 to see updated Inventory. "); }
void displayInventory(AUTO inventory[], int esize){ int i; if(esize == 0){ printf("No autos in inventory! "); return; } printf("List of autos in inventory: "); for(i = 0; i < esize; i++){ printf("%d %d %s %s %s %s %.2f ", i+1, inventory[i].year, inventory[i].make, inventory[i].model, inventory[i].color, inventory[i].condition, inventory[i].price); } }
void saveAutos(){ FILE *fp = fopen("inventory.bin", "wb"); if (fp == NULL){ printf("Error occured while opening file. Contact tech support. "); return; } fwrite(inventory, esize, fp); fclose(fp); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
