File Reading/Writing problems

Discussion in 'Programming & Scripting' started by mojobojo, Aug 16, 2009 with 2 replies and 332 views.

  1. mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    So im using this code. Works until I get to 0x109 then all of the bytes in the file are 0xCD. I have no clue how to fix this.
    Code:
    fstream FileOpen("C:\\Binary.bin");
    FileOpen.seekg(0, ios::end);
    int FileSize = FileOpen.tellg();
    FileOpen.seekg(0, ios::beg);
    char* FileBuffer = new char[FileSize];
    FileOpen.read(FileBuffer, FileSize);
    char* GBuffer = new char[FileSize];
    for (int i = 0; i < FileSize; i++)
    {
    GBuffer[i] = FileBuffer[i];
    }
    fstream NewFile("C:\\NewFile.bin", ios::out);
    NewFile.write(GBuffer, FileSize);
    NewFile.flush();
    NewFile.close();


     
  2. unknown v2

    unknown v2 VIP VIP

    Messages:
    3,229
    Ratings:
    1,241
    0xCD is the default byte when you do this " char* FileBuffer = new char[FileSize];", what seems to me. If you are working with binary files try adding | ios::binary to stream parameters.

    Also, what is the value of the FileSize int?
     
    • Like Like x 1
  3. OP
    mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    I set a breakpoint and checked, it is the same as the true file size (647168 bytes)

    EDIT: Setting the mode to Binary fixed it. Thanks.
     

Share This Page