I have written a function that takes input from a text file called "fridge.txt" with its contents being:
milk carton 4 2 .5
bread loaf 2 2 1
VB slab 20 40 10
I can read it into an array fine, and print it fine but the for loop I use needs to stop when it reaches the end of the file i.e after it gets to "10". It is fine when I know how many lines there are in fridge.txt but it is dynamic so instead of having "i < 4", I need to use some EOF function so if there were 20 it would print out 20 lines not 4. I tried using fscanf(...) != EOF but it skips the first line, prints the second, skips the third, prints the fourth.
Code:
void PrintInventory()
{
//Declare and initialise variables required
int i = 0;
struct Inventory list[50];
//Declare inventoryList as a file
FILE *inventoryList; //Open fridge.txt for reading
inventoryList = fopen("fridge.txt", "r");
printf("CURRENT INVENTORY:\n\n");
//Read contents of fridge.txt into array List until eof
for(i = 0; i < 4; i++)
{
fscanf(inventoryList,"%s %s %f %f %f", &list[i].productName, &list[i].measure, &list[i].currentAmount, &list[i].optimalAmount, &list[i].dangerousAmount);
printf("Item Name: %s\nCurrent Amount: %.1f %s<s>\n\n", list[i].productName, list[i].currentAmount, list[i].measure);
}
//Closes inventoryList for reading
fclose(inventoryList);
system("pause");
}
Im not quite sure how to do this but any help would be greatly appreciated.
.ZG.