Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fwrite() File Corruption C++

I'm somewhat of a newbie to C++ (moving from C#) so I'm not exactly sure what's going on here. What I'm trying to do is read an image out of a file and write it to an output file, but whenever I do parts of the file appear to be corrupt.

I've checked the data in memory and it actually matches, so I believe the culprit has to be something going on with fwrite(), although it could always just be something I'm doing wrong.

Here's some sample data: http://pastebin.com/x0eZin6K

And my code:

// 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;
like image 569
Lander Avatar asked Aug 08 '11 01:08

Lander


People also ask

Why is my file getting corrupt?

File corruption usually happens when there is an issue during the 'save' process. If your computer crashes the file being saved will likely be corrupted. Another cause might be damaged sections of your hard drive or storage media that might have viruses and malware.

Can a corrupted file be uncorrupted?

A corrupted file is always unusable and inoperable. When facing this issue, you can first try to repair them or try to run a virus detection program. However, if they do not work, you can try a third-party file recovery tool - Recoverit Data Recovery program to help recover corrupted files in Windows.

Can Corrupt my file be detected?

A corrupted file is not readable if you double-click on it; you'll often see an error message instead. Malware such as ransomware and file or disk wipers can even cause intentional, malicious file corruption. (See our support article, “What is a Corrupted File? “)


2 Answers

Are you running this code on Windows? For files that don't need text translation, you must open them in binary mode:

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

This is what happens in your output file:

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

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

The C runtime library helpfully translated your \n to \r\n.

like image 148
Greg Hewgill Avatar answered Sep 28 '22 00:09

Greg Hewgill


You need to open the file as a binary by adding "b" to the mode.

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

like image 41
jveazey Avatar answered Sep 28 '22 01:09

jveazey