0

can't figure out what's going wrong with my code. It's compiling without any errors but still it doesn't create any jpg file or any file at all.

Here's my code:

int main (int argc, char* argv [])
{
    {
        if (argc != 2)
        fprintf (stderr, "Usage: ./recover card.raw\n");
        return 1;
    }
    //Filename
    char* file = argv [1]; //ptr to char
    //Open file
    FILE *fileptr = fopen (file, "r"); //ptr to file, new variable
    if (fileptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", file);
        return 2;
    }
   //Buffer for reading
typedef uint8_t BYTE; 
    BYTE buffer[512]; 

    int img_count = 0; //Keeping track of the numbers of imgs, initializing their counter
    FILE *img = NULL; //new jpg file


    //reading file
    while (fread (buffer, 512, 1, fileptr) ==1) // data, size, number, inptr

{

    //checking first pixels to find out if JPG
    if (buffer [0] == 0xff &&
        buffer [1] == 0xd8 &&
        buffer [2] == 0xff &&
        (buffer [3] & 0xf0) == 0xe0)


    {
        //Is the first jpg?
        if(img == NULL)
        {
            //Creating new file
            char filename [8]; //creating an array to store 8 characters (###.jpg + \0)
            sprintf (filename, "%03i.jpg", img_count);
            img = fopen (filename, "w");
            img_count ++;

            //write jpeg to the created img
            fwrite(&buffer, 512, 1, img);
        }

        //Not the first jpg

        else
        {
            //close last jpg
            fclose(img);

             //Creating new file
            char filename [8]; //creating an array to store 8 characters (###.jpg + \0)
            sprintf (filename, "%03i.jpg", img_count);
            img = fopen (filename, "w");
            img_count ++;

            //write jpeg to the created img
            fwrite(&buffer, 512, 1, img);
        }
    }
//closing files
fclose (fileptr);
fclose (img);
}

return 0;
}

Here's the check50 answer:

check50

0

Here's the start of the code:

int main (int argc, char* argv [])
{
    {
        if (argc != 2)
        fprintf (stderr, "Usage: ./recover card.raw\n");
        return 1;
    }

Look carefully at the placement of curly braces. The if statement will cause the fprintf statement to execute when argc isn't 2.

More importantly, the return statement isn't in a code block associated with the if statement, so it will ALWAYS execute! You can confirm this with the command echo $? in the terminal window. It will display the return code from the last program executed.

Once this is fixed, there are more issues. There's no code to handle 512 byte blocks that don't have signatures, and the fclose statements at the end are within the while loop, causing more problems.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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