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);
}