0

Please help me by reviewing the code below. Cant seem to crack it.

Errors are as below: :( recovers 000.jpg correctly timed out while waiting for program to exit :( recovers middle images correctly timed out while waiting for program to exit :( recovers 049.jpg correctly timed out while waiting for program to exit

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

#define block_size 512
typedef uint8_t BYTE;

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

    //open memory card and read
    FILE* inptr = fopen(infile, "r");

    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    BYTE buffer[512]; //Define buffer with array of 512
    bool first_jpeg = false;
    FILE* new_jpeg;
    int counter = 0;

    while(fread(buffer, sizeof(BYTE), block_size, inptr) != 0x00) //repeat reading until end of memory card
    {
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //if found jpeg
        {
            if(!first_jpeg) //check whether it is 1st jpeg
            {
                first_jpeg = true;

                char filename[8];
                sprintf(filename, "%03i.jpeg", counter++);
                new_jpeg = fopen(filename, "w");

                if(new_jpeg == NULL)
                {
                    return 3;
                }
                fwrite(buffer, sizeof(BYTE), block_size, new_jpeg);
            }
            else //for subsequent jpeg, close previous file and then open new file
            {
                fclose(new_jpeg);
                char filename[8];
                sprintf(filename, "%03i.jpeg", counter++);
                new_jpeg = fopen(filename, "w");

                if(new_jpeg == NULL)
                {
                    return 4;
                }
                fwrite(buffer, sizeof(BYTE), block_size, new_jpeg);
            }
        }
        else //if already found a jpeg
        if(first_jpeg)
        {
            fwrite(buffer, sizeof(BYTE), block_size, new_jpeg);
        }
    }
    fclose(new_jpeg);  // close last jpeg fil
    fclose(inptr);  // close infile
    return 0;
}

1
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //if found jpeg
    {
        if(counter != 0) //check whether it is 1st jpeg
        {
            fclose(new_jpeg);
        }
            char filename[8];
            sprintf(filename, "%03i.jpg", counter);
            new_jpeg = fopen(filename, "w");
            counter++;

            if(new_jpeg == NULL)
            {
                return 3;
            }
            fwrite(buffer, sizeof(buffer), 1, new_jpeg);


    }
    else if(counter != 0)
  • Hi, can you please paste the revised code for me to understand better? – Karan Dhanak May 11 '20 at 12:37
  • I'd rather not to post digested solutions. Try *.jpg instead of jpeg (filename is still [8]), sizeof(buffer) instead of sizeof(BYTE) and move you counter outside - on a new line – Alex Void May 11 '20 at 13:18
  • it worked fine with jpg correction. Thanks. – Karan Dhanak May 11 '20 at 13:40

You must log in to answer this question.

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