0

Getting closer on Pset4 Recover but still working through some issues. I am now generating jpegs (yay!) but they apparently do not match the original images. So I am getting three errors:

:( recovers 000.jpg correctly recovered image does not match

:( recovers middle images correctly recovered image does not match

:( recovers 049.jpg correctly recovered image does not match

Can anyone tell where I am going wrong?

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>

int main(int argc, char *argv[])
{
    // Check to make sure user submits just 1 command-line argument
    if (argc != 2)
    {
        return 1;
    }

    // Define file pointer and open card.raw file entered by user
    FILE *fptr = fopen(argv[1], "r");
    if (!fptr)
    {
        return 1;
    }

    // Counter for number of jpegs
    int ctr = 0;

    // Open space in memory to read bytes to
    unsigned char bytes[512];

    // Open space in memory for new jpeg filenames
    char filename[4];
    
    // Create pointer for new jpeg files
    FILE *img = NULL;

    do
    {
        // Read 512 bytes from card.raw file
        fread(bytes, 1, 512, fptr);

        // Look for the start of jpeg files
        if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
        {
            // If this if the first jpeg
            if (ctr == 0)
            {
                // Create new filename for jpeg file
                sprintf(filename, "%03i.jpg", ctr);

                // Open new file to hold jpeg
                img = fopen(filename, "w");
                
                // Write data from buffer to file
                fwrite(bytes, 1, 512, img);

                // Add to counter
                ctr = ctr + 1;
            }

            // If this is not the first jpeg
            else
            {
                // Close previous jpeg
                fclose (img); 
                
                // Create new filename for jpeg file
                sprintf(filename, "%03i.jpg", ctr);

                // Open new file to hold jpeg
                img = fopen(filename, "w");
                
                // Write data from buffer to file
                fwrite(bytes, 1, 512, img);

                // Add to counter
                ctr = ctr + 1;
            }
        }
        
        // Add to existing jpeg file
        if (!(bytes[0] != 0xff && bytes[1] != 0xd8 && bytes[2] != 0xff && (bytes[3] & 0xf0) != 0xe0))
        {
            // Write data from buffer to file
            fwrite(bytes, 1, 512, img);
        }
    }
    while (!feof(fptr));

    // Close card.raw file
    fclose(fptr);
}
0

I have spotted 3 errors. Firstly, you should open the file for appending instead of writing. Secondly, you don't have a case where in if the first image hasn't been found and the first 4 bytes of 'bytes' are not of a header file. Thirdly, you should close the last image.

Hope this solves your problem. If it does, then please check the tickmark.

| improve this answer | |
  • thanks! I should open the file for writing rather than appending when it is the start of a new jpeg though, right? and just re-open the file to append when I'm adding to an existing jpeg? – getsendy Sep 30 at 19:13
  • For that you will have to close the file first. So it would be better to open it directly for appending and then close once done. This will save file size and boost performance. – Vsjain Oct 1 at 14:03

You must log in to answer this question.

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