2

ハフマンエンコーダーを書き込もうとしていますが、圧縮エラーが発生します。この問題は、ofstreamにput()された文字と、同じファイルのread()文字との不一致であると特定しました。

この問題の1つの特定の例:

  • 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);

コードはWindows7で実行されており、プログラム内のすべてのストリームはバイナリモードで開かれています。

4

3 に答える 3

5

タイプミスのためにバイナリモードで開かない:

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

する必要があります:

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

|、ではありません||

于 2012-01-08T08:40:22.787 に答える
3

The symptoms show that you are creating the ofsteam with text mode or you are creating it using a filedesc that is opened in text mode. You will want to pass ios::binary to it at construction time or it may run in text mode on Windows.

After you added the code, the reason proves to be a typo;

std::ios::binary||std::ios::ate

should be

std::ios::binary|std::ios::ate
于 2012-01-08T08:33:13.487 に答える
0

On Windows, if you are writing binary data, you need to open the file with the appropriate attributes.

Similarly, if you are reading binary data, you need to open the file with the appropriate attributes.

于 2012-01-08T08:34:15.430 に答える