[英]how to read/write sequnce of bits bit by bit in c++
我已经在 C++ 中实现了霍夫曼编码算法,并且运行良好。 我想创建一个文本压缩算法。
在数字世界的每一个文件或数据的背后,都有 0/1。
我想在文件中保留由霍夫曼编码算法生成的位序列( 0/1 )。
我的目标是保存要存储的文件中使用的位数。 我将用于解码的元数据存储在单独的文件中。 我想将数据逐位写入文件,然后在 c++ 中逐位读取相同的数据。
我在使用二进制模式时面临的问题是它不允许我一点一点地放置数据。 我想将“10101”逐位放入文件中,但它一次放入 asci 值或每个字符的 8 位。
#include "iostream"
#include "fstream"
using namespace std;
int main(){
ofstream f;
f.open("./one.bin", ios::out | ios::binary);
f<<"10101";
f.close();
return 0;
}
任何帮助或帮助指针表示赞赏。 谢谢你。
“二进制模式”仅表示您已请求您写入的实际字节不会被行尾转换损坏。 (这只是 Windows 上的问题。没有其他系统需要故意破坏您的数据。)
您仍然以二进制模式一次写入一个字节。
要写入位,请将它们累积在 integer 中。 为方便起见,在未签名的 integer 中。 这是您的位缓冲区。 您需要决定是从最不重要的位置到最重要的位置还是从最重要的位置到最不重要的位置来累积它们。 一旦累积了八位或更多位,就将一个字节写入文件,然后从缓冲区中删除这八位。
完成后,如果缓冲区中还剩下一些位,则将最后的 1 到 7 位写入一个字节。 您需要仔细考虑您是如何做到这一点的,以及如何知道有多少位,以便您可以正确解码另一端的位。
累积和提取是使用您的语言中的位操作完成的。 在 C++(和许多其他语言)中,这些是&
(和), |
(或)、 >>
(右移)和<<
(左移)。
例如,要在缓冲区中插入一位x
,然后在y
中插入三位,最后以最重要的位置中的最早位结束:
unsigned buf = 0, bits = 0;
...
// some loop
{
...
// write one bit (don't need the & if you know x is 0 or 1)
buf = (buf << 1) | (x & 1);
bits++;
...
// write three bits
buf = (buf << 3) | (y & 7);
bits += 3;
...
// write bytes from the buffer before it fills the integer length
if (bits >= 8) { // the if could be a while if expect 16 or more
// out is an ostream -- must be in binary mode if on Windows
bits -= 8;
out.put(buf >> bits);
}
...
}
...
// write any leftover bits (it is assumed here that bits is in 0..7 --
// if not, first repeat if or while from above to clear out bytes)
if (bits) {
out.put(buf << (8 - bits));
bits = 0;
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.