0

can someone tell me what is wrong with my code it works and recovers but no images are found upon checking :) recover.c exists. :) recover.c compiles. :) handles lack of forensic image :( recovers 000.jpg correctly 000.jpg not found :( recovers middle images correctly 001.jpg not found :( recovers 049.jpg correctly 049.jpg not found

#define block_size 512

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

// check the user uses program correctly
if (argc != 2)
{
    printf("Usage ./recover image\n");
    return 1;
}




// open memory card file
FILE *file = fopen(argv[1], "r");

// check
if (file == NULL)
{
    return 1;
}

 // declare variables
typedef uint8_t BYTE;   // variables  BYTE

// reading a block of 512 in memory card
BYTE buffer[block_size];
FILE *current_file = NULL;
char file_name[100];
bool first_JPEG = false;
size_t read_bytes;
int file_count = 0;
bool already_found = false;


// repeat until end of card
while (true)
{
   // read 512 bytes into a buffer
    read_bytes = fread(buffer, sizeof(BYTE), block_size, file);

    if (read_bytes == 0)
    {
        break;
    }

   // if start of a new JPEG
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        already_found = true;
        // if first JPEG
        if (!first_JPEG)
        {
            first_JPEG = true;
        }
        else
        {
           // close currnt file
            fclose(current_file);
        }
        // open new file and start writing
        sprintf(file_name, "%03i.jpeg", file_count);
        current_file = fopen(file_name, "w");
        fwrite(buffer, sizeof(BYTE), read_bytes, current_file);
        file_count++;
    }
    else
    {
        // If aleady found
        if (already_found)
        {
            // continue writing
            fwrite(buffer, sizeof(BYTE), read_bytes, current_file);
        }
    }
}
// close any remaining files
fclose(file);
fclose(current_file);
return 0;

}

1

what's the difference between a filename of *.jpg and *.jpEg??? ;-)

close isn't good enough. Results have to be exact. ;-)

2
  • Thanks alot it worked that was really stupid – YoussefSaeed Nov 19 '20 at 12:00
  • Don't beat yourself up. ;-) You've learned an important lesson - it's critical to pay attention to the smallest details! Happy programming! – Cliff B Nov 19 '20 at 19:30

You must log in to answer this question.

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