I've got a C program that encounters errors when I enter a while loop.
I initialize a variable (fragmentcount) and write into it using fscanf and assign it a value of 4 (this works)
int fragmentCount;
if ((fscanf(fp, "%i", &fragmentCount)) == 1) {
...
}
However, when I try to access it in a while loop below, fragmentCount = 0
while ((fscanf(fp, "%[#]", discards)) != EOF) {
printf(fragmentCount); // <- pseudocode
}
For a brief experiment, I tried taking away the fscanf as the conditional test for the while loop, and fragmentCount was the correct value (4).
Why is this so? How can I avoid this?