将位数据输出到二进制文件 C++
我正在编写一个压缩程序,需要使用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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将位收集到整个字节中,例如 unsigned char 或 std::bitset(其中位集大小是 CHAR_BIT 的倍数),然后一次写入整个字节。计算机“处理位”,但可用的抽象(尤其是对于 IO)是,作为程序员,您处理单个字节。按位操作可用于切换特定位,但您始终处理字节大小的对象。
在输出的末尾,如果没有整个字节,则需要决定如何存储它。 iostream 和 stdio 都可以分别使用 ostream::write 和 fwrite 写入未格式化的数据。
而不是单个字符或位集<8> (8 是 CHAR_BIT 最常见的值),您可以考虑使用更大的块大小,例如 4-32 个或更多字符的数组或同等大小的位集。
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.
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.
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.
对于编写二进制文件,我发现最有用的技巧是将所有二进制文件作为单个数组存储在内存中,然后将其全部移动到硬盘驱动器。一次执行一位、一次执行一个字节、或一次执行 unsigned long long 不如将所有数据存储在数组中并使用“fwrite()”的一个实例将其存储到硬盘。
size_t fwrite ( const void * ptr, size_t 大小, size_t 计数, FILE * 流 );
参考: http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/
英文:
fwrite( [存储数据的数组*], [数组 OBJECT 的大小(以字节为单位)。对于无符号字符 -> 1,对于无符号长整型 -> 8], [数组中的实例数] ,[文件*])
请务必检查您的退货是否成功验证!
此外,可以提出这样一个论点:让对象类型尽可能大是最快的方法([unsigned long long] > [char])。虽然我不熟悉“fwrite()”背后的编码,但我觉得从代码中使用的自然对象转换为 [unsigned long long] 与写入结合起来比“fwrite()”需要更多的时间“用你所拥有的去偿还。
当我学习霍夫曼编码时,我花了几个小时才意识到 [char] 和 [unsigned char] 之间的区别。请注意,对于此方法,您应该始终使用无符号变量来存储纯二进制文件。
For writing binary, the trick I have found most helpful is to store all the binary as a single array in memory and then move it all over to the hard drive. Doing a bit at a time, or a byte at a time, or an unsigned long long at a time is not as fast as having all the data stored in an array and using one instance of "fwrite()" to store it to the hard drive.
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Ref: http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/
In English:
fwrite( [array* of stored data], [size in bytes of array OBJECT. For unsigned chars -> 1, for unsigned long longs -> 8], [number of instances in array], [FILE*])
Always check your returns for validation of success!
Additionally, an argument can be made that having the object type be as large as possible is the fastest way to go ([unsigned long long] > [char]). While I am not versed in the coding behind "fwrite()", I feel the time to convert from the natural object used in your code to [unsigned long long] will take more time when combined with the writing than the "fwrite()" making due with what you have.
Back when I was learning Huffman Coding, it took me a few hours to realize that there was a difference between [char] and [unsigned char]. Notice for this method that you should always use unsigned variables to store the pure binary.
通过下面的类,您可以一点一点地写入和读取
示例
by below class you can write and read bit by bit
for
example