Read content from a binary file, which contains integers?

2

as you can read the contents of a file ".bin" in which we have inserted whole numbers.

I would like to know how you can read that file, I used the "fread" tool but it does not show me anything or random numbers.

The ".bin" file contains the following with the hexdump command:

0000000 0020 0000 000b 0000 0061 0000 0012 0000
0000010 0017 0000 000a 0000                    
0000018

That is, they are integers numbers that I have previously entered using an array.

The goal is to be able to read those numbers, that is, that file.

This is the code of my program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
unsigned short lenght;
int i;
char * token;
int array[6];
float size;
FILE *file;


file= fopen("numeros.bin", "rb");

if (file==NULL)
{
printf("Error file not found\n");
return 1;
}

printf("-----------------------------\n");

printf("Quin nombre vols mostrar del fitxer binari:\n");
scanf("%u", &lenght );

fseek(file, lenght, SEEK_SET);
token=(char *)malloc(lenght+1);

fread( token, sizeof(lenght), 1, file);

printf("%x\n", token);


printf("-----------------------------\n");

fclose(file);

return 0;

}
    
asked by GokuGod97 31.05.2016 в 00:53
source

2 answers

1

Once you know (you know?) that each integer occupies 4 bytes (32 bits) ... and assuming there are no problems of big-endianess vs little-endianess, then it's just a matter of reading -and fseekear- by 4 bytes. For example:

#include<stdio.h>

int main(void)
{
        int pos, res, val;
        int width;
        width = sizeof(int); /* cuantos bytes ocupa cada entero en el archivo */
        FILE *file;
        file = fopen("numeros.bin", "rb");
        if (file == NULL) {
                printf("Archivo no se pudo abrir\n");
                return 1;
        }
        printf("-----------------------------\n");
        printf("Ingrese posicion a mostrar (0=primero)\n");
        scanf("%d", &pos);
        res = fseek(file, pos * width, SEEK_SET);
        if (res != 0) {
                printf("Error en seek");
                return 1;
        }
        fread(&val, width, 1, file);
        printf("val = 0x%x (decimal:%d)\n", val, val);
        printf("-----------------------------\n");
        fclose(file);
        return 0;
}
    
answered by 31.05.2016 в 14:07
1

If you want to read an integer:

int entero;
fread(&entero, sizeof(int), 1, file);

This copies the number of bits of an int to the integer address. To read an Array:

int enteros[x];
fread(enteros, sizeof(int), x, file);

The function fread what it does is copy in the direction that you say a number of elements of a certain size read from a file.

Thus, you could read a file with 6 integers such that:

int array[6];
FILE *in = fopen("fileName", "rb");
fread(array, sizeof(int), 6, in);
fclose(in);

If the file has not been created by you with your program you have to take into account the format of the stored integers (an int can have different sizes on different platforms and a 1 can be stored as 0x00000001 or as 0x01000000 depending on whether it is little endian or big endian).

    
answered by 12.06.2016 в 15:04