-1

How do I only save one or more items that matches my search of varunummer? This function works but if my array is limited and save the file the items saved before disappear and only the searched item/s is saved. Also how do I increase the nrOfGoods just with the nr of items matched? PS: nrOfGoods keeps track for the number of items in the array. This is what I got so far:

struct varor{
    int varunummer;
    char namn[WORDLENGTH];
    int lagersaldo;
};

void saveProduct(struct varor reg[], int nrOfGoods){
    FILE * fp;
    char nameFile[WORDLENGTH];
    int i, j, varunummer;

    printf("Enter varunummer: ");
    scanf("%d", &varunummer);
    for(i=0; i<nrOfGoods; i++){
        if ( reg[i].varunummer == varunummer){      
            printf("Enter file name to save (end with .txt): ");
            scanf("%s", nameFile);
            fp = fopen (nameFile, "w");

            //fprintf(fp,"%d\n", nrOfGoods);// this delete all my other saved items

            for(i=0;i<(nrOfGoods);i++){
                fprintf(fp,"%d\n", reg[i].varunummer);
                fprintf(fp,"%s\n", reg[i].namn);
                fprintf(fp,"%d\n", reg[i].lagersaldo);
            }
        } else printf("\nVarunummer not found!\n");
    }
    fclose(fp);

}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Zuzu
  • 19
  • 5
  • 1
    You're using the same variable `i` for both loops. Also, your `Varrunummer not found` message will be printed even if the number is found, because it prints it for each item that doesn't match, even if another item does. – Barmar Oct 24 '17 at 19:02
  • @Barmar yes I did that, but it erases all my content saved before. I think it is because of fprintf(fp,"%d\n", nrOfGoods); How should i write it instead? – Zuzu Oct 24 '17 at 19:05
  • @xing I need to add an item not to append the file – Zuzu Oct 24 '17 at 19:21

1 Answers1

0

You shouldn't be writing the items in a second loop. Just write the item that matches.

You also need to open the file before the loop. When you open a file in w mode it empties the file first, so each time through the loop is deleting the previous iteration.

And you shouldn't print Varunnummer not found inside the loop, since you can't tell if the item isn't found until you go through the entire array. Use a variable that you set when you find a matching item; if you never find anything, it will not be updated, and you can print a message at the end. See Searching array reports "not found" even though it's found

struct varor{
    int varunummer;
    char namn[WORDLENGTH];
    int lagersaldo;
};

void saveProduct(struct varor reg[], int nrOfGoods){
    FILE * fp;
    char nameFile[WORDLENGTH];
    int i, j, varunummer;
    int found = 0;

    printf("Enter varunummer: ");
    scanf("%d", &varunummer);
    printf("Enter file name to save (end with .txt): ");
    scanf("%s", nameFile);
    fp = fopen (nameFile, "w");
    for(i=0; i<nrOfGoods; i++){
        if ( reg[i].varunummer == varunummer){      
            fprintf(fp,"%d\n", reg[i].varunummer);
            fprintf(fp,"%s\n", reg[i].namn);
            fprintf(fp,"%d\n", reg[i].lagersaldo);
            found = 1;
        }
    }
    fclose(fp);
    if (!found) {
        printf("\nVarunummer not found!\n");
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612