将非标准数据类型写入C中的文件

时间:2021-12-20 22:40:35

I have a C structure that contains several int data types; I shift and mask the data types in this structure to produce 73 bits of ordered data. I need to write many lines of these 73 bits into a txt file in format "0x73bits" using fprintf.

我有一个包含几种int数据类型的C结构;我移动并屏蔽此结构中的数据类型以生成73位有序数据。我需要使用fprintf将这些73位的多行写入格式为“0x73bits”的txt文件中。

It must be exactly 73 bits of data every time in just a few lines of code. I can do this using 64 bits then playing around with 9 bits. But it is messy.

每次只需几行代码,它就必须是73位数据。我可以使用64位然后使用9位进行操作。但它很混乱。

Can anyone suggest a more clean professional method?

谁能建议更干净的专业方法?

1 个解决方案

#1


2  

You can not write bits to a file ,only bytes. You will have to leave empty bits at the end of the file.Also in memory your structure is represented in bytes not bits.

您不能将位写入文件,只能写入字节。您将不得不在文件的末尾留下空位。在内存中,您的结构以字节而不是位表示。

You can convert your bits data into a string, this will waste disk space but might be what you want. here is an example of printing a binary string:printf binary

您可以将您的位数据转换为字符串,这将浪费磁盘空间,但可能是您想要的。这是打印二进制字符串的示例:printf binary

This is an example of fprintf to a file:fprintf to a file

这是fprintf到文件的示例:fprintf到文件

#1


2  

You can not write bits to a file ,only bytes. You will have to leave empty bits at the end of the file.Also in memory your structure is represented in bytes not bits.

您不能将位写入文件,只能写入字节。您将不得不在文件的末尾留下空位。在内存中,您的结构以字节而不是位表示。

You can convert your bits data into a string, this will waste disk space but might be what you want. here is an example of printing a binary string:printf binary

您可以将您的位数据转换为字符串,这将浪费磁盘空间,但可能是您想要的。这是打印二进制字符串的示例:printf binary

This is an example of fprintf to a file:fprintf to a file

这是fprintf到文件的示例:fprintf到文件