0

To start, I've come a long way because I have gotten lots of help looking over all other questions & answers here, thank you!!

Currently, I can run recover.c successfully, and it will recover images 000 through 048, but not image 049. All images look correct and I can view them. Before I reached this impasse, I had only been able to recover two images, and I figured out how to fix this by increasing the allocation for my filename array (file[7] --> file[8]). Also, the images that are at the end (image 048) was my number 2 image before (image 001).

Any help is greatly appreciated!

{ // ** * recover.c * * Computer Science 50 * Problem Set 5 * * Recovers JPEGs from a forensic image. */

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


typedef uint8_t BYTE ;

// create struct for JPEG header
typedef struct
{
        BYTE  buffer[512];
} __attribute__((__packed__))
BUFFER; 

// check to see if JPEG found
int found(BUFFER check)
{    
        if (check.buffer[0] == 0xff && check.buffer[1] == 0xd8 && check.buffer[2] == 0xff && (check.buffer[3] == 0xe0 || check.buffer[3] == 0xe1))
        {
                return 0;
        }
        else   
        {
                return 1;
        }
} 

int main(void)
{
        // open the card.raw file for reading
        FILE* inptr = fopen("card.raw", "r");

        // create JPEG number
     int number = 0;

     // create JPEG file name and open it
     char file[12];
        sprintf(file, "%.03d.jpg", number);
        FILE* outptr = fopen(file, "w+");

        // create a BUFFER to link data to
        BUFFER photo;


        // check for EOF
        while (feof(inptr) == 0) 
        {    
                // read one block into BUFFER
            fread(&photo, sizeof(BUFFER), 1, inptr); 

                // if image found, copy one block at a time until a new image is found
                if (found(photo) == 0)
                {
                    if (number == 0)
                    {
                            number++;
                            fwrite(&photo, sizeof(BUFFER), 1, outptr);
                            fread(&photo, sizeof(BUFFER), 1, inptr);
                            while (found(photo) != 0)
                        {
                                fwrite(&photo, sizeof(BUFFER), 1, outptr);
                                fread(&photo, sizeof(BUFFER), 1, inptr);
                            }
                    }
                    else
                    {
                        fclose(outptr);
                        sprintf(file, "%03d.jpg", number);
                        number++;
                        FILE* outptr = fopen(file, "w+");
                        fwrite(&photo, sizeof(BUFFER), 1, outptr);
                }           
            }
                else if (number != 0 && found(photo) == 1)
                {
                    fwrite(&photo, sizeof(BUFFER), 1, outptr);
                    continue;
                }      
        }
        return 0;            
}

}

1
  • It is not good style to share your entire code online: someone could take it and figure out how to fix it, then you'd have someone who only did about 30% of the work and you'd still have no idea how to fix it.
    – ChiCubed
    Dec 30, 2014 at 13:35

1 Answer 1

1

I forgot to reset my position after exiting the loop upon completing copying the first JPEG. I had this code in before I changed the allocation for my filename array to file[8], and for some reason I thought it was working better without it.

I also updated the file to close all open files. Hopefully this is good, but for now, I am satisfied!

1
  • Can you please add that as a comment in your original question, or maybe close the original question?
    – ChiCubed
    Dec 30, 2014 at 13:34

You must log in to answer this question.

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