0

I am at PSet4-recover. I can't compile code properly.

Always get these errors :

 recover.c:47:31: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]
        img = fopen(filename, 'w');
                              ^~~
/usr/include/stdio.h:247:30: note: passing argument to parameter '__modes' here
                    const char *__restrict __modes) __wur;
                                           ^
recover.c:60:31: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]
        img = fopen(filename, 'w');
                              ^~~
/usr/include/stdio.h:247:30: note: passing argument to parameter '__modes' here
                    const char *__restrict __modes) __wur;
                                           ^
2 errors generated.
make: *** [<builtin>: recover] Error 1 

this 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 = NULL;
  // check if reach ennd of memory card
  while( fread(bytes, sizeof(BYTE), 512, input) >= 512)
  {
    // read 1 block from card
    fread(bytes, sizeof(BYTE), 512, 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);
      }
    }
  }
  return 0;
}
1

You need to use double quotes, not single quotes. 'w' is understood by the compiler as the char w, while "w" is understood as "write-mode". (Remember that chars also have an integer value that the compiler can convert them to, which is why it is calling this an integer error.) So, change the quote type and that should clear up the error. You can read more about it on its page in the CS50 manual.

Hope this helps. If it does, feel free to check click the check mark next to my answer. But if not, let me know and I will try to help further.

6
  • Well, first thank you for your caring. Second, It really worked but problem is it doesn't work too. My code produce only 2 corrupted images. here is the new version of Code : cs50.stackexchange.com/questions/41731/… Jul 27 at 3:51
  • This solved this issue. But, often, fixing one issue will bring others to the surface. :) The issues you are having in that other question are common ones with this pset, which tends to give people the most issues, and it looks like Cliff has already provided some guidance on correcting those. Jul 27 at 12:19
  • Well thanks Robert. I have solved it. Aug 8 at 8:52
  • Glad you have gotten the pset to work! Don't forget to accept the answers that have provided solutions to your questions to keep the up on site maintenance (and mark the question as solved). Aug 8 at 14:12
  • Thanks for the advice. How can I mark the question as solved? Aug 9 at 7:58

You must log in to answer this question.

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