2

I am tasked to create an inventory program that stores data using structure and save the data into a binary file. The program should then be able to load the data i.e. the structure elements from the binary file. I am able to save the input data to a binary file using the fwrite function; however, I am having a problem in loading the file to the program using the fread function.

I already looked for similar problems but I can't find a similar case wherein the fread is used to pass the structure elements saved to the structure variable in the program.

This is the code for my structure

struct details {
    char name[30];
    double price;
    int code;
    int qty;
};

details item[SIZE];

Function in adding an item to the inventory

int AddItem(int n){
    details inputitem;
    printheader();
    printf("Item Name: ");
    scanf("%s", inputitem.name); //check %[^\n]
    printf("\nItem Price: ");
    scanf("%lf", &inputitem.price);
    printf("\nItem Code: ");
    scanf("%d", &inputitem.code);
    printf("\nQuantity: ");
    scanf("%d", &inputitem.qty);
    printf("\nItem Added! The ID of the item is %d.\n", n+1);
    item[n] = inputitem;
}

Function in displaying the inventory

int DisplayInventory(int n){
    printheader();
    printf("ID |    NAME           |   CODE   |  QUANTITY |  PRICE \n");
    printf("------------------------------------------------------------\n");
    for(int i=0; i<n; i++){
        printf("%d   %-18s   %-10d  %-10d  %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price);
    }
}

Function in saving to a binary file

int SaveFile(int n){
    FILE *fp;
     fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","wb");

      if(!fp) {
             printf("Cannot open file.\n");
             system("pause");
             exit(1);
      }


      for(int i=0; i<n; i++){
          fwrite(&item[i], sizeof(struct details), 1, fp);
      }



      fclose(fp);
      printf("File is saved!\n");

}

Function in loading the binary file

int LoadFile(int n){
     FILE *fp;
     fp=fopen("C:\\Users\\Royce\\Documents\\CPEPROG2 Goden Final Project\\Inventory.txt","rb");

      if(!fp) {
             printf("Cannot open file.\n");
             system("pause");
             exit(1);
      }


      struct details inputitem;
      int i = 0;
      while(fread(&inputitem, sizeof(struct details),1, fp)){

           item[i] = inputitem; 

           i++;
/*    printf("%d   %-18s   %-10d  %-10d  %-10lf \n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price); */

      }
      fclose(fp);

      printf("File is loaded!\n");  
}

I expect the program to show in the DisplayInventory function the details of the structure saved in the binary file. However, nothing shows up at all.

When i try to print the structure in the LoadFile function (using the commented line), all variables show 0.

EDIT Main Function

int main (){



    int choice; //gets the choice of user from the menu
    bool condition = 1; //loops the menu
    //details item[50]; 
    int count=0; //counts the number of items in the inventory

    do{
        printheader(); //prints the title of the program
        printmenu(); //prints the menu (list of commands)
        scanf("%d", &choice);
        switch(choice){
            case 1: system("cls");
                    AddItem(count);
                    count++; 
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 2: system("cls");
                    //EditItem();
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 3: system("cls");
                    //DeleteItem();
                    system("PAUSE"); 
                    system("cls");
                    break;          
            case 4: system("cls");
                    //ViewItem();
                    system("PAUSE"); 
                    system("cls");
                    break;  
            case 5: system("cls");
                    DisplayInventory(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 6: system("cls");
                    SaveFile(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 7: system("cls");
                    LoadFile(count);
                    system("PAUSE"); 
                    system("cls");
                    break;
            case 8: printf("\nThank you!");
                    exit(0);
                    break;
            default: printf("\nInvalid Input!\n");  
                     getch();
                     system("cls");     

        }
    }while(condition = 1);


    return 0;
}
8
  • 1
    The LoadFile() function does not return any value. Should it be i the number of records? Please take note of compiler warnings, which should also expose the missing function return value in all the other functions posted here. Commented Jul 1, 2019 at 18:32
  • details item[SIZE]; it won't compile because of missing keyword struct, are you sure you have given the right code? Commented Jul 1, 2019 at 18:36
  • Are you using a C compiler or a C++ compiler? Commented Jul 2, 2019 at 3:42
  • The values of the fields got printed when i put it before the i++ line. However, i still can't "load" them in the DisplayInventory function. I don't get it. Shouldn't the item[] structure array be updated in the load function since item[] is a global variable? Commented Jul 2, 2019 at 6:52
  • Also about the function return value, should functions always return a value even if they're not void? I use int functions because I need to pass the value of the number of records from the main function to the functions. I didn't add a return value because well I did not need to return any value to the main function. Please correct me if I'm wrong, I'm not really that good in programming yet. Thanks! Commented Jul 2, 2019 at 6:57

1 Answer 1

0

The program never increments the 'count' variable, so when 'DisplayInventory' is called the value is always 0 and nothing is displayed :

return the i variable in 'LoadFile()' and assign it to the 'count' variable in the main() scope : in LoadFile :

    ...
    printf("File is loaded!\n");  
    return i;
}

in main :

...
case 7: system("cls");
    count = LoadFile(count);
    system("PAUSE"); 
    system("cls");

Note that the 'n' argument in 'int LoadFile(int n)' is not used, you could remove it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.