我正在尝试编写霍夫曼编码器,但遇到一些压缩错误。我将问题确定为put()到ofstream的字符与同一文件中的read()字符之间不匹配。

此问题的一个特定实例:

  • put()写入ASCII字符10(换行)
  • read()读取ASCII字符13(回车)

  • 我以为读写原始数据(没有字符翻译)就可以了,我不确定为什么会这样。有人可以帮我吗?

    这是用于编写压缩文件的ofstream实例:
    std::ofstream compressedFileStream(getCompressedFileName(),std::ios::binary||std::ios::ate);
    

    和用于读取相同内容的ifstream实例
        std::ifstream fileInput(getFileName()+".huf",std::ios::binary);
    

    该代码在Windows 7上运行,并且该程序中的所有流都以二进制模式打开。

    最佳答案

    由于输入错误,无法以二进制模式打开:

    std::ofstream compressedFileStream(getCompressedFileName(),std::ios::binary||std::ios::ate)
    

    应该:
    std::ofstream compressedFileStream(getCompressedFileName(),std::ios::binary|std::ios::ate)
                                                                          //   ^
    
    |,而不是||

    关于c++ - 放置和读取的字符之间不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8776284/

    10-11 22:58
    查看更多