0

i was working on pset4 recover and my code finds all the image, but everytime it writes to the image the image got corrupted, and i cant find the issue, checked the debugger and it seems going the way i intended, what is the faulty part?

int main(int argc, char *argv[])
{

    FILE *MemoryCard;
    FILE *CurrentOpenFile;
    uint8_t JpegBlocks[512] = {0};
    char StrFileHolder[7];
    int FileCounter = 0;

    if (argc != 2)
    {
        printf("[...] Can only Recover 1 Memory Card at a time.\n");
        return 1;
    }

    MemoryCard = fopen(argv[1], "r");

    if (MemoryCard == NULL)
    {
        printf("Fail to Open. Check file extension (should be .raw).\n");
        return 1;
    }

    while (fread(&JpegBlocks, 512, 1, MemoryCard) == 1)
    {

        if ((JpegBlocks[0] = 255) && (JpegBlocks[1] == 216) && (JpegBlocks[2] == 255) && (JpegBlocks[3] >= 224) && (JpegBlocks[3] <= 239))
        {

            if (FileCounter == 0)
            {

                sprintf(StrFileHolder, "%03i.jpg", FileCounter);
                CurrentOpenFile = fopen(StrFileHolder, "w");
                fwrite(&JpegBlocks, 512, 1, CurrentOpenFile);
                FileCounter++;

            }
            else
            {

                fclose(CurrentOpenFile);
                sprintf(StrFileHolder, "%03i.jpg", FileCounter); 
                CurrentOpenFile = fopen(StrFileHolder, "w");
                fwrite(&JpegBlocks, 512, 1, CurrentOpenFile);
                FileCounter++;

            }

        }
        else if (FileCounter != 0)
        {
            fwrite(&JpegBlocks, 512, 1, CurrentOpenFile);
        }

    }
    fclose(CurrentOpenFile);
    fclose(MemoryCard);

}
1
  1. This (JpegBlocks[0] = 255) uses the set operator, not the comparison operator.
  2. This allocation char StrFileHolder[7]; won't accomodate the null terminator when sprintf creates the string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .