0

Help to recover images on this program. Thank you.

/***************************************
 * recover.c
 * problem pset4
 * computer science 50
 * Reccvers JPEGs from a foreinsic image
 * **************************************/
 #include <stdio.h>
 #include <stdint.h>

 // unsigned integers type
 #define BLOCKSIZE  512
 typedef uint8_t BYTE;

 int main(int argc, char* argv[])
 {
     // open memory card
     FILE *file = fopen("card.raw", "r");
     if (file == NULL)
     {
         printf("Cannot open file\n");
         return 1;
     }

     // open output file
     FILE *outfile = NULL;
     // create new image file
     int jpgCount = 0;

     // forever loop that that will end when the file has been reached
     while(1)
     {
         // array of type BYTE
         BYTE image[BLOCKSIZE] = {};

         // iterate over byte in each block untill the end of file
         for(int i = 0; i < BLOCKSIZE; i++)
         {
             // close the file when the file is ended
             if(feof(file))
             {
                 fclose(file);
                 if(outfile != NULL)
                 {
                     fclose(outfile);
                 }
                 return 0;
             }

             // read one byte at a time
             fread(&image[i], sizeof(BYTE), 1, file);
         }

         // first four bytes match those of jpg
         if(image[0] == 0xff &&
            image[1] == 0xd8 &&
            image[2] == 0xff &&
            image[3] == 0xe0 &&
            image[3] == 0xe1)
         {
             // close outfile if it is exit
             fclose(outfile);
             // an array of filename
             char filename[4];
             // name file using ###.jpg format
             sprintf(filename, "%3d.jpg", jpgCount);
             jpgCount++;

             // open outfile
             outfile = fopen(filename, "w");
             if(outfile == NULL)
             {
                 printf("Could not write images\n");
                 return 1;
             }

             // photo written to the outfile
             fwrite(&image, sizeof(BYTE) * BLOCKSIZE, 1, outfile);
        }    
     }
     // close file
     fclose(file);
 }
3
  • What is the problem with the current version of your program? What did you try to solve that? And how can we help you? Commented Oct 10, 2018 at 13:53
  • check the conditions where you are comparing the first 4 bytes to match those of jpg. particularly, img[3] == 0xe0 && img[3] == 0xe1 which will always result in a false cause img[3] cannot have both 2 values at once. watch the walkthrough once carefully and you will get your answer for this. Commented Oct 10, 2018 at 14:33
  • Thank you very much I did check my condition and now I got where I did a mistake and I am watching the Work through again for more details. Again thank you very much. Commented Oct 11, 2018 at 9:16

1 Answer 1

0

The error message you describe is due to your code not testing argc. You are meant to complain if no file name is passed, and use argv[1] as the file name if it is provided. card.raw is just the sample card image, the actual file name might differ.

And look up how bitmasks work, they are great in testing whether a number's first hexadecimal digit is e.

2
  • My problem is that the program cannot find the images. Help me to fix to find the images. I really thank you very much. Commented Oct 11, 2018 at 9:23
  • That's not the problem mentioned in the question. And I already told you to look up bitmasks, they are really convenient. Or use like ... && image[2] == 0xff && image[3] >= 0xe0 && image[3] <= 0xef, but I'd still recommend checking out bit masks. And 4 bytes are not enough for a 7 character file name.
    – Blauelf
    Commented Oct 11, 2018 at 10:53

You must log in to answer this question.

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