将位数据输出到二进制文件C++

6
我正在编写一个压缩程序,需要使用c++将位数据写入二进制文件。如果有人能提供关于写入语句的建议或者相关网站的建议,我会非常感激。
如果这是一个简单或令人困惑的问题,我很抱歉。我在网上找不到答案。
3个回答

3
将位合并成整个字节,例如无符号字符或std::bitset(其中位集大小是CHAR_BIT的倍数),然后一次写入整个字节。计算机“处理位”,但可用的抽象 - 特别是对于IO - 是您作为程序员处理单个字节。按位操作可用于切换特定位,但您始终处理字节大小的对象。
在输出末尾,如果您没有整个字节,您需要决定应如何存储它。iostreams和stdio都可以使用ostream :: write和fwrite分别编写未格式化的数据。
您可以考虑使用更大的块大小,例如4-32个字符或等效大小的位集的数组,而不是单个char或bitset <8>(8是CHAR_BIT的最常见值)。

谢谢Fred的建议,这让我知道了该去哪里寻找。 - Drew C

2
对于二进制的写入,我发现最有帮助的技巧是将所有的二进制数据存储为一个单一的数组在内存中,然后将其全部移动到硬盘上。按位,按字节或按无符号长整型进行处理,不如将所有数据存储在一个数组中,并使用一个“fwrite()”实例将其存储到硬盘中来得快。
size_t fwrite (const void * ptr, size_t size, size_t count, FILE * stream);
参考:http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/ 用英文描述:
fwrite([存储数据的数组*],[数组对象的大小(以字节为单位)。对于无符号字符-> 1,对于无符号长整数-> 8],[数组中的实例数],[文件*])
始终检查返回值以验证成功!
此外,可以提出一种论点,即使将对象类型尽可能大也是最快的方式([unsigned long long]> [char])。虽然我不熟悉“fwrite()”背后的编码,但我认为将自然对象从您的代码转换为[unsigned long long]所需的时间将比“fwrite()”花费更多时间,因此建议使用当前已有的对象。
回顾学习霍夫曼编码时,我花了几个小时才意识到[char]和[unsigned char]之间的差异。请注意,对于此方法,您应始终使用无符号变量来存储纯二进制数据。

1
通过下面的类,您可以逐位编写和读取位。
class bitChar{
public:
    unsigned char* c;
    int shift_count;
    string BITS;

    bitChar()
    {
        shift_count = 0;
        c = (unsigned char*)calloc(1, sizeof(char));
    }

    string readByBits(ifstream& inf)
    {
        string s ="";
        char buffer[1];
        while (inf.read (buffer, 1))
        {
            s += getBits(*buffer);
        }
        return s;
    }

    void setBITS(string X)
    {
        BITS = X;
    }

    int insertBits(ofstream& outf)
    {
        int total = 0;

        while(BITS.length())
        {
            if(BITS[0] == '1')
                *c |= 1;
            *c <<= 1;
            ++shift_count;
            ++total;
            BITS.erase(0, 1);

            if(shift_count == 7 )
            {
                if(BITS.size()>0)
                {
                    if(BITS[0] == '1')
                        *c |= 1;
                    ++total;
                    BITS.erase(0, 1);
                }

                writeBits(outf);
                shift_count = 0;
                free(c);
                c = (unsigned char*)calloc(1, sizeof(char));
            }
        }

        if(shift_count > 0)
        {
            *c <<= (7 - shift_count);
            writeBits(outf);
            free(c);
            c = (unsigned char*)calloc(1, sizeof(char));
        }
        outf.close();
        return total;
    }

    string getBits(unsigned char X)
    {
        stringstream itoa;
        for(unsigned s = 7; s > 0 ; s--)
        {
            itoa << ((X >> s) & 1);
        }

        itoa << (X&1) ;
        return itoa.str();
    }

    void writeBits(ofstream& outf)
    {
        outf << *c;
    }

    ~bitChar()
    {
        if(c)
            free(c);
    }
};

例如:

对于示例

#include <iostream>
#include <sstream>
#include <fstream>
#include <string> 
#include <stdlib.h>
using namespace std;


int main()
{
    ofstream outf("Sample.dat");
    ifstream inf("Sample.dat");

    string enCoded = "101000001010101010";

    //write to file
    cout << enCoded << endl ; //print  101000001010101010
    bitChar bchar;
    bchar.setBITS(enCoded);
    bchar.insertBits(outf);

     //read from file
    string decoded =bchar.readByBits(inf);
    cout << decoded << endl ; //print 101000001010101010000000
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接