I have to read a binary file and i just created one binarytext.txt and inserted into it the following sequence of characters.
00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0000000000000
Now i have to read this file.So what i do is
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char *argv[])
{
FILE *fp;
int i;
fp = fopen(argv[1], "rb");
if(fp == NULL)
{
printf("\n Unable to open the file. Aborting");
exit(EXIT_FAILURE);
}
if(argc < 1)
{
printf("\n You must enter the name of the binary file from which to read the data");
exit(EXIT_FAILURE);
}
fread(&i, sizeof(i), 1, fp);
printf("\n Data that was read from the binary file is %d", i);
fclose(fp);
return 0;
}
Now i was expecting the input to be 0 (all the bits are 0 in the file and fread would read 4 bytes). But what i am getting output is some very huge number
Data that was read from the binary file is 808464432Press any key to continue .
. .
Why is it so? Am i doing something wrong ? Because i am just opening the filein rb mode and reading 32 bits using fread.