Problem: The issue I'm having is that I have a file with 20 integers, and all I'm trying to do is have those 20 integers read. Which occurs in this program... except that there's an additional value inserted at the start, which is whatever int num;
is initialized as. Even if I remove initialization, I'm given a default integer value of 3739648
.
How can I remedy this?
int main() {
FILE* fp = fopen("20integers.txt", "r");
int num =0;
fseek(fp, 0, SEEK_SET);
if(fp == NULL) {
return 1;
}
//fseek(fp, 5, SEEK_SET);
while (!feof (fp))
{
printf ("%d ", num);
fscanf (fp, "%d", &num);
}
fclose (fp);
return 0;
}
Output:
0 1 20 17 1 14 12 9 88 61 16 7 11 7 6 31 47 3 47 18 2
Process finished with exit code 0
The first '0
' should not be there.