I've realized that my much bigger file is failing because it can't properly read the first integer in a binary file. This is my test file I've set up to do only that. I know that the int I'm reading will always be 1 byte so I read the data into a char and then cast it as a short. It got this working at some point in the past but I somehow messed it up when cleaning up my code.
At this point the program is printing
"The integer is 127"
When it should be printing
"The integer is 1"
Does anybody know why this may be?
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *inp;
char r;
short i;
if ((inp = fopen(argv[0],"r")) == NULL){
printf("could not open file %s for reading\n",argv[1]);
exit(1);}
fread((void *)&r,(size_t) 1,(size_t) 1, inp);
i = (short)r;
printf("The integer is %d\n",i);
}
sizeof(int)
no matter how you keep it in memory. – Irritated