reading from a binary file into an int
Solution 1
That is not a binary file, it's a text file, try this instead
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file;
int value;
char text[33];
char *endptr;
file = fopen("file2", "r");
if (file == NULL) /* check that the file was actually opened */
return -1;
if (fread(text, 1, sizeof(text) - 1, f1) != sizeof(text) - 1)
{
fclose(file);
return -1;
}
text[32] = '\0';
value = strtol(text, &endptr, 2);
if (*endptr == '\0')
printf("%d", value);
fclose(file);
return 0;
}
To write a binary file you need this
#include <stdio.h>
void hexdump(const char *const filename)
{
FILE *file;
unsigned char byte;
file = fopen(filename, "rb");
if (file == NULL)
return;
fprintf(stdout, "hexdump of `%s': ", filename);
while (fread(&byte, 1, 1, file) == 1)
fprintf(stdout, "0x%02x ", byte);
fprintf(stdout, "\n");
}
int main()
{
const char *filename;
FILE *file;
int value;
filename = "file.bin";
file = fopen(filename, "wb");
if (file == NULL)
return -1;
value = 7;
if (fwrite(&value, sizeof(value), 1, file) != 1)
fprintf(stderr, "error writing to binary file\n");
fclose(file);
/* check that the content of the file is not printable, i.e. not text */
hexdump(filename);
file = fopen(filename, "rb");
if (file == NULL)
return -1;
value = 0;
if (fread(&value, sizeof(value), 1, file) != 1)
fprintf(stderr, "error writing to binary file\n");
else
fprintf(stdout, "The value read was %d\n", value);
fclose(file);
return 0;
}
as you will see from the example above, the stored data in file.bin
is not in text format, you cannot inspect it with a text editor because the bytes 0x00
and 0x07
are not printable, in fact 0x00
is the nul
byte which is used in c to mark the end of a string.
Solution 2
This is one way you can write and read to a binary file. you should know the difference between a binary and ASCII file, before reading from them. Read this once https://stackoverflow.com/a/28301127/3095460 and understand what is type of file you are reading using your code.
if you open a file in an editor and see 00000000000000000000000000000111
it does not mean its a binary file, most of the editor process files as ascii text file only. you need a binary editor to open a binary file and read meaningful data from them.
#include <stdio.h>
int main()
{
FILE *Read_fptr = NULL;
FILE *Write_fptr = NULL;
int data = 20;
size_t nElement = 1;
if ( (Write_fptr = fopen("data.bin", "wb")) != NULL ) {
if ( fwrite(data, nElement, sizeof data, Write_fptr) != sizeof data ) {
fprintf(stderr,"Error: Writing to file\n");
fclose(Write_fptr);
return -1;
}
} else {
fprintf(stderr,"Error: opening file for writing\n");
return -1;
}
fclose(Write_fptr);
data = 0;
if ( (Read_fptr = fopen("data.bin", "rb")) != NULL ) {
if ( fread(data, nElement, sizeof data, Read_fptr) != sizeof data) {
if( !feof(Read_fptr) ) {
fprintf(stderr,"Error: Reading from file\n");
fclose(Read_fptr);
return -1;
}
}
} else {
fprintf(stderr,"Error: opening file for reading\n");
return -1;
}
fclose(Read_fptr);
printf("%d\n",data);
return 0;
}
mike
Updated on June 26, 2022Comments
-
mike almost 2 years
i'm a little puzzled about binary files and how to read from them, so if someone could help that would be great. i have a file that contains the following bits:
00000000000000000000000000000111
(32 bits. i counted)
now i have written this code:
int main() { FILE * f1; f1 = fopen("file2", "rb"); int i = 0; fread(&i, sizeof(int), 1, f1); printf("%d", i); fclose(f1); return 0; }
that prints me 808464432. why? shouldnt it print 7? thank you for reading.