I have a txfile.txt file which has the following data:
3
2716 16253
7721 35149
972 2614
I am trying to read the first integer into a variable using this code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num_cust[1];
int result;
FILE *fp;
if( (fp = fopen("tsfile.txt", "r")) == NULL)
{
perror("Error opening file\n");
exit(1);
}
result = fread(&num_cust[0], sizeof(int), 1, fp);
if(result != 1)
{
printf("read error\n");
exit(1);
}
printf("num_cust = %d\n", num_cust[0]);
fclose(fp);
}
I expected num_cust to be 3 (the first integer in the file). But the printout shows it to be 856306231. Wondering what can be wrong.