简体   繁体   English

从bin文件的缓冲区中解析数据c

[英]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. 我有一个包含一些数据的bin文件,我可以读取该数据并将其存储在变量中。 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 输出:53324477812552451219223312232012122211305213462334644247717440148531711811913243 34437515052573583

What i want is to be able to access every number separately. 我想要的是能够分别访问每个号码。 I tried: printf("%s", buffer[0]); 我试过: 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 . 第一个是你将指针传递给指向fread的指针。 The other is that you read an integer into a char buffer, ie a string. 另一种是你将一个整数读入一个char缓冲区,即一个字符串。 The third is that buffer is not allocated and points to a random location in memory. 第三个是buffer未分配并指向内存中的随机位置。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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