2011-08-08 35 views
4

私はC++の初心者です(C#から移行しています)ので、ここで何が起こっているのか正確にはわかりません。私がやろうとしているのは、イメージをファイルから読み込んで出力ファイルに書き込むことですが、ファイルの一部が壊れているように見えるときはいつでもそうです。fwrite()ファイルの破損C++

私はメモリ内のデータをチェックして実際に一致しています。だからこそ、fwrite()で何か起こっていなければならないと思います。 http://pastebin.com/x0eZin6K

そして、私のコード:高度で

// used to figure out if reading in one giant swoop has to do with corruption 
    int BlockSize = 0x200; 
    // Read the file data 
    unsigned char* data = new unsigned char[BlockSize]; 
    // Create a new file 
    FILE* output = fopen(CStringA(outputFileName), "w+"); 
    for (int i = 0; i < *fileSize; i += BlockSize) 
    { 
     if (*fileSize - i > BlockSize) 
     { 
      ZeroMemory(data, BlockSize); 
      fread(data, sizeof(unsigned char), BlockSize, file); 
      // Write out the data 
      fwrite(data, sizeof(unsigned char), BlockSize, output); 
     } 
     else 
     { 
      int tempSize = *fileSize - i; 
      ZeroMemory(data, tempSize); 
      fread(data, sizeof(unsigned char), tempSize, file); 
      // Write out the data 
      fwrite(data, sizeof(unsigned char), tempSize, output); 
     } 
    } 
    // Close the files, we're done with them 
    fclose(file); 
    fclose(output); 
    delete[] data; 
    delete fileSize; 

おかげ

ここではいくつかのサンプルデータです!

答えて

9

Windowsでこのコードを実行していますか?テキスト翻訳を必要としないファイルの場合、あなたはバイナリモードで開く必要があります。

07 07 07 09 09 08 0A 0C 14 0D 0C 

07 07 07 09 09 08 0D 0A 0C 14 0D 0C 
        ^^ 

Cランタイムライブラリは、親切にあなたの\n翻訳:

FILE* output = fopen(CStringA(outputFileName), "wb+"); 

これはあなたの出力ファイルに何が起こるかであります\r\n

+0

Ah!初期ファイルをバイナリモードで開きましたが、出力ファイルは開きませんでした。あなたは素晴らしいです、ありがとう! – Lander

関連する問題