简体   繁体   中英

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.

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, ie 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM