输出位数据的二进制文件C ++ [英] Outputting bit data to binary file C++

查看:133
本文介绍了输出位数据的二进制文件C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个COM pression程序,需要位数据写入用c ++的二进制文件。如果任何人都可以在写的语句,或者咨询网站建议,我将非常感激。

I am writing a compression program, and need to write bit data to a binary file using c++. If anyone could advise on the write statement, or a website with advice, I would be very grateful.

道歉,如果这是一个简单的或混乱的问题,我苦苦寻找的网页答案。

Apologies if this is a simple or confusing question, I am struggling to find answers on web.

推荐答案

收集位转换成全部字节,例如为unsigned char或std :: bitset的(其中的bitset大小CHAR_BIT的倍数),然后写全字节在一个时间。计算机处理位,但可用的抽象 - 尤其是对IO - 就是你,作为一个程序员,对付单个字节。位操作可以用来切换特定的位,但你总是处理字节大小的物体。

Collect the bits into whole bytes, such as an unsigned char or std::bitset (where the bitset size is a multiple of CHAR_BIT), then write whole bytes at a time. Computers "deal with bits", but the available abstraction – especially for IO – is that you, as a programmer, deal with individual bytes. Bitwise manipulation can be used to toggle specific bits, but you're always handling byte-sized objects.

在输出的最后,如果你没有一个整体的字节,你需要决定如何应该存储。无论iostream和stdio时分别使用的ostream ::写入和fwrite,写格式化数据。

At the end of the output, if you don't have a whole byte, you'll need to decide how that should be stored. Both iostreams and stdio can write unformatted data using ostream::write and fwrite, respectively.

而不是单个字符或位集< 8>(8是为CHAR_BIT最常见的值),你可能会如4-32,或更多字符数组或同等大小的考虑使用较大的块大小,位集。

Instead of a single char or bitset<8> (8 being the most common value for CHAR_BIT), you might consider using a larger block size, such as an array of 4-32, or more, chars or the equivalent sized bitset.

这篇关于输出位数据的二进制文件C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆