I have a question regarding reading input from a file.
The input file (input.txt):
Code:
8 9 2.5
10 5 2.6
7 15 9.5
0 0 0
1
1 8
My program:
Code:
#include <stdio.h>
#define MAX_TBL_SIZE 30
int main()
{
FILE *ifp;
char name[10];
int index = 0;
int ind2;
int id[MAX_TBL_SIZE], stock[MAX_TBL_SIZE];
float price[MAX_TBL_SIZE];
// Reads user input for filename. 10 character long string. Guaranteed to be correct filename.
printf("Enter the name of the input file:\n");
scanf("%s", name);
printf("\n");
// Opens the filename entered
ifp = fopen(name, "r");
// Adds variables from file into specified arrays
while(id[index] != 0 && stock[index] !=0 && price[index] != 0.0)
{
fscanf(ifp, "%d", &id[index]);
fscanf(ifp, "%d", &stock[index]);
fscanf(ifp, "%f", &price[index]);
index++;
}
// Prints current index value
printf("DEBUGGER: INDEX = %d\n", index);
// Prints each value of array id untill it reaches the index vaule
for (ind2 = 0; ind2 < index; ind2++)
{
printf("DEBUGGER: id[%d] = %d", ind2, id[ind2]);
printf("\n");
}
// Same as above, except for stock array
for (ind2 = 0; ind2 < index; ind2++)
{
printf("DEBUGGER: stock[%d] = %d", ind2, stock[ind2]);
printf("\n");
}
// Repeated for price array
for (ind2 = 0; ind2 < index; ind2++)
{
printf("DEBUGGER: price[%d] = %.2f", ind2, price[ind2]);
printf("\n");
}
// Shows current location in file
fscanf(ifp, "%d", &num);
printf("CURRENT LOCATION fscanf: %d\n", num);
// Closes file
fclose(ifp);
system("PAUSE");
return 0;
}
What I want my program to do is to add these variables to 3 different arrays, but stop once it reaches the three zero string (0 0 0).
When it compiles, it seems my index value is stopping too soon. Each time it only seems to accumulate to 2 before stopping, rather than stopping once it reaches the marker.
Any ideas on what is going on here?