0
~/workspace/pset4/recover/ $ check50 cs50/2017/x/recover    

:) 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 015.jpg correctly
015.jpg not found

Hopefully I could get any hint about the problem in my code? Thanks in advance.

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

  // ensure proper usage
  if (argc != 2)
  {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
  }
  else
  {
        return 0;
  }

// open input file
char *infile = argv[1];
FILE *inptr = fopen(infile, "r");
if (inptr != NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

  char buffer[512];
  int i = 0;// filename sequential number
  char* filename = NULL;
  int number = 0;//# of recovered images
  FILE *img;

  while(fread(buffer, 512, 1, inptr) ==1)
  {
     if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
     {
           if(number >0)
           {
                 fclose(img);
           }
           i++;
           sprintf(filename, "%03i.jpg", i);
           img = fopen(filename, "w");
           //fwrite(buffer, 512, 1, img);
           number++;
     }
     else if(number > 0)
     {
          fwrite(buffer, 512, 1, img);
     }
  }

  fclose(inptr);
  //fclose(img);
  }
1

While there are several problems, one problem is preventing the program from doing anything. Interestingly, this is the first time that I've seen a bug allow a program successfully compile when there are other compile bugs.

Look at the following code:

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

  // ensure proper usage
  if (argc != 2)
  {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
  }
  else
  {
        return 0;
  }

This code will always either return 1 because of incorrect usage, or will terminate with return 0; when correctly implemented. No matter what, the code will never execute past this code block. Instead of return 0, it should just continue.

Once this is fixed, the compiler isn't going to be happy about the choice of char as the data type for the buffer. Perhaps you should look for another data type, one that represents an unsigned integer of 1 byte size?

There are other issues, including the one that @curiouskiwi pointed out. Since this bug has prevented you from even seeing them, I'll let you try to fix them first. If you have questions about new issues, please post a new question.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1

When you run the program yourself, do you get the files? And do you get 000.jpg as the first file?

Looks like your first file is probably 001.jpg because you increment i before you use it in your name.

i++;
sprintf(filename, "%03i.jpg", i);
1
  • I thought JPEG name starts from 001.jpg. Thanks for pointing it out.
    – juanli
    Sep 9 '17 at 5:48

You must log in to answer this question.

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