// This is the method in question
int ReadItems(FILE *input, int *count, Item *items)
{
int weight=-1;
int value=-1;
int cost=-1;
int capacity=32; // Initial capacity for the array
char buffera[160 + 1];
// Try to allocate memory for the array
if((items = malloc(sizeof(Item) * capacity)) == NULL)
{
printf("Couldn't allocate the item array");
return -1;
}
// read the header information from the file. This line clears out excess junk before the actual data
fscanf(input, "%30s %30s %30s", buffera, buffera, buffera);
// Set the number of items in the list equal to zero
*count=0;
// Scan in the information bit by bit. Yes i know that i have those "OR"ed instead of "AND"ed, this is for debugging.
while((fscanf(input, "%d", &weight) == 1) || (fscanf(input, "%d", &value) == 1) || (fscanf(input, "%d", &cost) == 1))
{
// Print the details out to the screen so i can see what's being scanned in
printf("\nValues: %d %d %d", value, weight, cost);
// Store the details in my array
items[*count].weight = weight;
items[*count].value = value;
items[*count].cost = cost;
*count = *count + 1;
}
if(count == 0)
return -1;
printf("\nTest: %d", *count);
fflush(stdout);
return 1;
}
// This is the input file
Weight value Cost
1 7 4
5 6 10
11 6 15
4 5 8
2 4 20
6 3 8
4 2 9
10 2 3
6 1 7
// This is the output of the screen. As you can see, scanf("%d") isn't ignoring whitespace...
Values: -1 1 -1
Values: -1 7 -1
Values: -1 4 -1
Values: -1 5 -1
Values: -1 6 -1
Values: -1 10 -1
Values: -1 11 -1
Values: -1 6 -1
Values: -1 15 -1
Values: -1 4 -1
Values: -1 5 -1
Values: -1 8 -1
Values: -1 2 -1
Values: -1 4 -1
Values: -1 20 -1
Values: -1 6 -1
Values: -1 3 -1
Values: -1 8 -1
Values: -1 4 -1
Values: -1 2 -1
Values: -1 9 -1
Values: -1 10 -1
Values: -1 2 -1
Values: -1 3 -1
Values: -1 6 -1
Values: -1 1 -1
Values: -1 7 -1