【问题标题】:Byte output to binary file C++字节输出到二进制文件 C++
【发布时间】:2015-09-28 16:42:13
【问题描述】:

我正在编写霍夫曼编码,一切正常,直到我尝试将结果保存到存档文件中。我们的老师让我们用这样的功能来做(每次都需要一点时间,其中8个应该输出一个字节):

long buff=0;
int counter=0;
std::ofstream out("output", std::iostream::binary);

void putbit(bool b)
{
    buff<<=1;
    if (b) buff++;
    counter++;
    if (counter>=8)
    {
        out.put(buff);

        counter=0;
        buff=0;    
    }
}

我尝试了一个输入这样的位序列的示例:

0011001011001101111010010001000001010101101100

但二进制模式的输出文件只包含:1111111

由于buff 变量的数字正确(25 102 250 68 21 108),我建议我在笔记本中写错了代码,这行有问题:

out.put(buff);

我试图用这一行删除它:

out << buff;

但得到:1111111111111111 另一种方法是:

out.write((char *) &buff, 8);

给出:

100000001000000010000000100000001000000010000000

它看起来最接近正确答案,但仍然无法正常工作。

也许我对文件输出有些不理解。

问题:

您能解释一下如何使它工作以及为什么以前的变体是错误的吗?

更新: 输入来自这个函数:

void code(std::vector<bool> cur, std::vector<bool> sh, std::vector<bool>* codes, Node* r)
{
    if (r->l)
    {
        cur.push_back(0);
        if (r->l->symb)
        {
            putbit(0);
            codes[(int)r->l->symb] = cur;

            for (int i=7; i>=0; i--)
            {
                if ((int)r->l->symb & (1 << i))
                putbit(1);
                else putbit(0);
            }
        }
        else
        {
        putbit(0);
        code(cur, sh, codes, r->l);
        }
        cur.pop_back();
    }
    if (r->r)
    {
        cur.push_back(1);
        if (r->r->symb)
        {
            putbit(1);
            codes[(int)r->r->symb] = cur;

            for (int i=7; i>=0; i--)
            {
                if ((int)r->r->symb & (1 << i))
                putbit(1);
                else putbit(0);
            }
        }
        else
        {
        putbit(1);
        code(cur, sh, codes, r->r);
        }
        cur.pop_back();
    }
}

【问题讨论】:

  • 我看到该代码唯一的错误是您没有检查函数的返回值是否有错误。
  • 我刚刚在您的输入上运行了您的代码,它生成了正确的文件。你能展示你用来调用你的函数的代码吗?
  • 我实际上运行了您的代码并且它可以工作,尽管我会使用 char 而不是 long 作为缓冲区变量。也许向我们展示您是如何在代码中实际使用此函数的?另外,“正确的数字(25 102 250 68 21 108)”是什么?你应该告诉我们你想要达到的目标。
  • asig 和 Ishamael,您能说出哪些变体有效吗? asig,我的意思是二进制操作正常工作,并且在输出到文件 buff 之前具有正确的值
  • @Tami 我直接复制粘贴了你的函数,out.put( buff )。我现在明白你的意思了。

标签: c++ file output byte


【解决方案1】:

问题是,您的 putbit 函数正在工作(虽然它很糟糕,但您使用全局变量并且您的缓冲区应该是 char)。 例如,这就是我测试您的功能的方式。

out.open( "outfile", std::ios::binary ); 
if ( out.is_open() ) {
    putbit(1);
    putbit(1);
    putbit(0);
    putbit(1);

    putbit(0);
    putbit(1);
    putbit(0);
    putbit(0);

    out.close();
}

这应该以十六进制输出 1101 0100d4

我相信这是一个 XY 问题。您要解决的问题不在于 putbit 函数,而在于您使用它的方式和算法。

您说在将数据放入输出文件之前您的值是正确的。 stackoverflow中有很多类似的问题,你自己找吧。

真正的问题是 putbit 功能不足以解决您的问题。您依赖这样一个事实,即在您调用它 8 次后它会写入一个字节。如果你写少于 8 个字节怎么办?此外,您永远不会刷新文件(至少在您发布的代码中),因此无法保证所有数据都会被写入。

首先您必须了解文件句柄(流)的工作原理。在本地打开你的文件,检查它是否打开,完成后关闭它。关闭还保证文件缓冲区中的所有数据都写入文件。

outfile.open( "output", std::ios::binary );
if ( outfile.is_open() ) {
    // ... use file ...
    outfile.close();
}
else {
    // Couldnt open file!
}

其他问题可以通过编写或使用 BitStream 来解决。它看起来有点像这样,

class OutBitstream {
public:
    OutBitstream();
    ~OutBitstream(); // close file

    bool isOpen();
    void open( const std::string &file );
    void close(); // close file, also write pending bits

    void writeBit( bool b ); // or putbit, use the names you prefer
    void writeByte( char c );

    void writePendingBits(); // write bits in the buffer they may
    // be less than 8 so you may have to do some padding

private:
    std::ofstream _out;
    char _bitBuffer; //or std::bitset<8>
    int _numbits;
};

使用这个接口应该更容易处理位输入。也没有全局变量。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多