Originally Posted by
laserlight
Yeah, that's what I meant when I said I realised that "antal_varor appears to be the number of entries read".
You're right to think about switching from a for loop to a while loop: a for loop tends to be used when you have a certain number of entries to read, whereas a while loop tends to be used when the looping is more indefinite.
I was able to change so that my savetofile inserts antal_varor
so that when it comes around to readfromfile it already contains the the right nr of antal_varor
but the new issue is that my output is still printing out zeros
Varunummer: Namn Lagersaldo
0 0
0 0
0 0
0 0
0 0
0 0
how do i make sure that my code prints out this
Varunummer: Namn Lagersaldo
123 majs 12
234 banan 45
432 klossar 32
654 klas 32
435 soppa 32
121 granat 23
Code:
void readFromFile(char file[], int *antal_varor, struct storagemanipulation *inventory)
{
int i;
printf("Enter the name of the file that you would like to open: \n");
scanf("%s", file);
char namn[30];
int lagersaldo, varunummer;
FILE *fp;
fp=fopen(file,"r");
if (fp!=NULL)
{
fscanf(fp, "%d", antal_varor);
for (int i = 0; i < *antal_varor; i++)
{
if(fscanf(fp, file, "%s %d %d", inventory[i].namn, &inventory[i].lagersaldo, &inventory[i].varunummer) == 3)
{
inventory[varunummer].varunummer = varunummer;
strcpy(inventory[i].namn, namn);
inventory[i].lagersaldo = lagersaldo;
}
}
}
else
{
printf("no access to the File\n");
}
fclose(fp);
}