xRed
xRed

Reputation: 1997

Parse data from buffer of a bin file c

I have a bin file wich contais some data, i am suposed to read that data and store it in variables. The problem is i dont know how to parse the data from the buffer.

FILE *file;
char *buffer;

//Abre o ficheiro
file = fopen("retail.bin", "rb");
if (!file)
{
    printf("Erro ao abrir %s\n", "retail.bin");
    return;
}

//Lê o conteúdo do ficheiro
while(fread(&buffer, sizeof(int), 1, file) == 1){
    printf("%d", buffer);
}

fclose(file);

Output: 53324477812552451219223312232012122211305213462334644247717440148531711811913243 34437515052573583

What i want is to be able to access every number separately. I tried: printf("%s", buffer[0]);

But the program stops working.

Upvotes: 0

Views: 407

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409432

You have a couple of problems. The first is that you pass a pointer to a pointer to fread. The other is that you read an integer into a char buffer, i.e. a string. The third is that buffer is not allocated and points to a random location in memory. The fourth is that you print a "string" as an integer.

If you want to read an integer, then read it into an integer:

int value;
fread(&value, sizeof(value), 1, file);
printf("%d", value);

Upvotes: 2

Related Questions