简体   繁体   English

在C中的x字节之后从二进制读取

[英]Reading from binary after x bytes in C

I am trying to read double values from a binary in C, but the binary starts with an integer and then the doubles I am looking for. 我正在尝试从C中的二进制文件读取double值,但二进制文件以整数开头,然后是我要查找的double。 How do I skip that first 4 bytes when reading with fread()? 使用fread()读取时如何跳过前4个字节? Thanks 谢谢

Try this: 尝试这个:

fseek(input, sizeof(int), SEEK_SET);

before any calls to fread . 在打电话给fread之前。

As Weather Vane said you can use sizeof(int) safely if the file was generated in the same system architecture as the program you are writing. 正如Weather Vane所说,如果文件是在与所编写程序相同的系统体系结构中生成的,则可以安全地使用sizeof(int) Otherwise, you should manually specify the size of integer of the system where the file originated. 否则,您应该手动指定文件起源系统的整数大小。

You can use fseek to skip the initial integer. 您可以使用fseek跳过初始整数。 If you insist on using fread anyway, then you can read the integer first: 如果您仍然坚持使用fread,则可以先读取整数:

fread(ptr, sizeof(int), 1, stream) . fread(ptr, sizeof(int), 1, stream)

Of course you have to declare ptr before calling fread . 当然,您必须在调用fread之前声明ptr

As I said, fseek is another option: 如我所说, fseek是另一种选择:

fseek(stream, sizeof(int), SEEK_SET) . fseek(stream, sizeof(int), SEEK_SET)

Beware that fseek moves the file pointer in bytes (1 in the given line from the beginning of the file); 注意fseek将文件指针移动到字节(从文件开头开始的给定行中为1); integer can be 4 or other number of bytes which is system specific. integer可以是4或其他特定于系统的字节数。

Be careful when implementing things like this. 实施这样的事情时要小心。 If the file isn't created on the same machine, you may get invalid values due to different floating point specifications. 如果文件不是在同一台计算机上创建的,则由于不同的浮点规范,您可能会得到无效的值。

If the file you're reading is created on the same machine, make sure that the program that writes, correctly address the type sizes. 如果您正在读取的文件是在同一台计算机上创建的,请确保所写的程序正确处理了类型大小。

If both writer and reader are developed in C and are supposed to run only on the same machine, use the fseek() with the sizeof(type) used in the writer in the offset parameter. 如果writer和reader都是用C语言开发的,并且只能在同一台机器上运行,请在offset参数中使用fseek()和writer中使用的sizeof(type)。

If the machine that writes the binary isn't the same that will read it, you probably don't want to even read the doubles with fread() as their format may differ due to possible different architectures. 如果写入二进制文件的机器与读取二进制文件的机器不同,则您甚至可能不想使用fread()读取双精度型,因为其格式可能会因体系结构不同而有所不同。

Many architectures rely on the IEEE 754 for floating point format, but if the application is supposed to address multi-platform support, you should make sure that the serialized format can be read from all architectures (or converted while unserializing). 许多体系结构都依赖于IEEE 754的浮点格式,但是如果应用程序应该解决多平台支持的问题,则应确保可以从所有体系结构中读取序列化格式(或在反序列化时进行转换)。

Just read those 4 unneeded bytes, like 只需读取这4个不需要的字节,例如

void* buffer = malloc(sizeof(double));
fread(buffer,4,1,input); //to skip those four bytes

fread(buffer,sizeof(double),1,input); //then read first double =)
double* data = (double*)buffer;//then convert it to double 

And so on 等等

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

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