#problem in using fscanf function

10 messages · Page 1 of 1 (latest)

chrome berryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#
#include <stdio.h>
#include <string.h>
typedef struct inventory {
  unsigned int id;
  char name[50];
  unsigned int quantity;
  unsigned int price;

} inventory;
int main() {
  // create a file, enter the records
  FILE* p = fopen("inventaire.txt", "w+");
  printf("please, write the number of products \n");
  int n;
  char t = ' ';
  scanf("%d", &n);
  inventory obj1[100];
  printf("please write the name then quantity and the price of each product\n");
  for (int i = 0; i < n; i++) {
    printf("the product %d:\n", i + 1);
    scanf("%s", &obj1[i].name);
    scanf("%d", &obj1[i].quantity);
    scanf("%d", &obj1[i].price);
    obj1[i].id = i;
    fprintf(p, "%d %c %s %c %d %c %d", obj1[i].id, t, obj1[i].name, t,
            obj1[i].quantity, t, obj1[i].price);
    fputs("\n", p);
  }
  // read file, update record fields
  fseek(p, 0, SEEK_SET);
  for (int i = 0; i < n; i++)
    fscanf(p, "%d %c %s %c %d %c %d %c", &obj1[i].id, &t, &obj1[i].name, &t,
           &obj1[i].quantity, &t, &obj1[i].price, &t);
  printf("write the number of product that you want to change it quantity\n");
  int number_of_apdate;
  scanf("%d", &number_of_apdate);
  for (int i = 0; i < number_of_apdate; i++) {
    char name[50];
    printf("write the name of the prodeuct\n");
    scanf("%s", name);
    for (int i = 0; i < n; i++) {
      if (strcmp(name, obj1[i].name) == 0) {
        printf("write the size that you want to add it\n");
        int size;
        scanf("%d", &size);
        obj1[i].quantity += size;
      }
    }
  }
  fseek(p, 0, SEEK_SET);
  for (int i = 0; i < n; i++) {
    fprintf(p, "%d %c %s %c %d %c %d", obj1[i].id, t, obj1[i].name, t,
            obj1[i].quantity, t, obj1[i].price);
    fputs("\n", p);
  }
  fclose(p);
}
empty knot
#

this code had a incorrect result

bold summit
#

"incorrect result" is not information that would allow to offer any help, you need to be specific

empty knot
empty knot
#

problem in using fscanf function

#

!solve

chrome berryBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

#

@empty knot

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

empty knot
#

!solve