0

everyone.

I've been struggling with the Recover problem of Pset4. I really don't understand why I'm getting the following error message when applying the Check50 control:

*Results for cs50/problems/2021/x/recover generated by check50 v3.2.2

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( 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*

Here's my code:

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


// Number of bytes in .jpeg header

typedef uint8_t HEADER [512];
int image_counter = 0; 
FILE *output_file = NULL; 


int main(int argc, char *argv[])
{
 
    // Check command-line arguments to see if input is valid

    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }
    
    // Determine if file can be opened
    
    FILE *memory_card = fopen(argv[1], "r");
    
    if (memory_card == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }
        
    
    // Loop through memory card (until end) to find JPEG headers. Read 512 blocks of data into a buffer. 


    char filename[8];
    HEADER header;
    fread(&header, sizeof(header), 1, memory_card);
     
    //Start of a JPEG file
    while (fread(&header, sizeof(header), 1, memory_card)==1)
    {
        if (header [0] == 0xff && header [1] == 0xd8 && header [2] == 0xff && (header [3] & 0xf0) == 0xe0)//JPEG found
            
        {
            if (!(image_counter == 0))//Not first JPEG found. Close previous one. Open JPEG found and copy data to a file. 
                
            {
                {   
                    fclose(output_file);
                    
                }
                sprintf(filename, "%03i.jpg", image_counter);
                output_file = fopen(filename, "w"); 
                image_counter++;
                fwrite(&header, sizeof(header), 1, output_file);
            }
                
            else//First JPEG found. Open it and copy data to a file. 
                 
            {
                sprintf(filename, "%03i.jpg", image_counter);
                output_file = fopen(filename, "w");
                image_counter++;
                fwrite(&header, sizeof(header), 1, output_file);
            }
        }
            
        else 
        {
            continue;
        }
            
    } 
                  
    //Close all files 

    fclose(memory_card);
    fclose(output_file);
        
    return 0;
}
    

If someone can help me with this, I would be very grateful!!!

Marina

0

This code looks for the start of a jpg, and closes the previous open file (if it exists) then starts a new file by adding the 512 Bytes of the current block. From the background section: "Realize, of course, that JPEGs can span contiguous blocks. Otherwise, no JPEG could be larger than 512 B." The case where there's not a new header, but already an open file needs to be considered.

You must log in to answer this question.

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