I know that fread is not meant to be used with non-binary files. However, I was wondering: If I provide the function with the correct size of the input, why shouldn't it return the correct values, but instead it returns garbage?
FILE *fp;
int val = 0;
if(fp = fopen("test.txt", "r")){
fread(&val, sizeof(int), 1, fp);
printf("val read %d \n", val);
fclose(fp);
}
if(fp = fopen("test.txt", "r")){
fscanf(fp, "%d", &val);
printf("val read %d \n", val);
fclose(fp);
}
As an example, if I read a file with inside only 1234567, the output is
val read 875770417
val read 1234567