0

I am at Pset4 recover pset. When I run my code, it produces only 2 corrupted images

here is my code :

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
  // validate input number
  if( argc != 2)
  {
    printf("Usage: ./recover filename. \n");
    return 1;
  }

    // validte recovery storage file
    FILE *input = fopen(argv[1], "r");
  if( input == NULL)
  {
    printf("Coudn't open file.\n");
    return 1;
  }

  // read from input file
  // placeholder from each img block == 512 bytes
  BYTE bytes[512];
  //images counter for naming
  int numOfImgs = 0;
  // placeholder for image name "string"
  char filename[8];
  // placeholder for image data to bestored before exporting to external file
  FILE *img;
  // check if reach ennd of memory card
  while( fread(bytes, sizeof(bytes), 1, input) > 0)
  {
    // read 1 block from card
    fread(bytes, sizeof(bytes), 1, input);
    // current block is a starting of valid jpeg image
    if( bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
    {
      // if JPEG found is the first one
      if( numOfImgs == 0)
      {
        // create a sequenced name
        sprintf(filename, "%03i.jpeg",numOfImgs);
        // open JPEG file to write in
        img = fopen(filename, "w");
        // write date in JPEG file
        fwrite(bytes, sizeof(bytes), 1, img);
        // increase number of found images
        numOfImgs++;
      } else
      {
        // if fond image is not the first which means there an image is currently open
        // close the current opened image
        fclose(img);
        // create a sequenced name
        sprintf(filename, "%03i.jpeg",numOfImgs);
        // open JPEG file to write in
        img = fopen(filename, "w");
        // write date in JPEG file
        fwrite(bytes, sizeof(bytes), 1, img);
        // increase number of found images
        numOfImgs++;
      }
    } else {
      // if current block is not a valid start for a JPEG
      // check if there is an open JPEG to add current block to
      if( numOfImgs >= 0)
      {
        // add current block to opened JPEG
        fwrite(bytes, sizeof(bytes), 1, img);
      }
    }
  }
  fclose(input);
  fclose(img);
  return 0;

}

1 Answer 1

1

This is the biggest problem in the code, and a very common error. I stopped looking once I found this. Look at this code:

 // check if reach ennd of memory card
  while( fread(bytes, sizeof(bytes), 1, input) > 0)
  {
    // read 1 block from card
    fread(bytes, sizeof(bytes), 1, input);

This is doing two sequential reads. The second one overwrites the first. That means that every other read is discarded without being processed.

Simple fix. Delete the second read.

There may be other problems, but this one is so big, I want you to have a chance to work through any others, assuming there are any. ;-)

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

6
  • OK first thanks for your answer. Second, this error causing corrupted images or producing only 2 images? Commented Jul 27, 2021 at 4:32
  • I only want to analyze what is wrong to avoid in the future Commented Jul 27, 2021 at 4:35
  • Well, after you fix the double read, if there are any other problems, please post a new question with the new code and new problems! This bug is causing most of the signature blocks to be missed. Also, missing every other block means that the file headers are likely getting corrupted. Certainly the images are corrupted because they're only getting every other block. There are a number of issues that this causes. Fix this first, to see what issues disappear!
    – Cliff B
    Commented Jul 27, 2021 at 6:48
  • Hint: there is another bug waiting in the wings after you fix the double read. It will likely produce a seg fault. ;-) Its more important that YOU get used to finding these errors - it's about developing your skills debugging programs!!! I could tell you what the bugs are, but you wouldn't gain experience finding them! Believe me, it's actually a really good thing to have lots of bugs in your programs NOW, so that you get lots of practice fixing them, while there are people around to help you figure it out vs. later when writing your own code and don't even recognize what's happening! :-)
    – Cliff B
    Commented Jul 27, 2021 at 6:54
  • The seg fault is a case where one error masks another. Something to learn now - if you fix a bug and all of a sudden you have lots more, it doesn't necessarily mean that you made it worse. It just means that you revealed some additional hidden problems that were masked! Happy coding! :-D
    – Cliff B
    Commented Jul 27, 2021 at 6:56

You must log in to answer this question.

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