As suggested, I am employing sprintf to write the file names for the recover assignment. I am not sure if I am using the pointer * correctly. The documentation for sprintf shows the first argument to be a character string with a pointer:

int sprintf(char *str, const char *format, ...)

I tried to apply this format to my code. I hope someone can confirm that I am using it correctly before moving on:

    typedef uint8_t BYTE;
    BYTE buffer[512];
    int counter = 0;
    while(fread(&buffer, 512,1, input) ==1)
    //check if there are four consecutive jpeg start bytes
    if (buffer[0] == 0xff  &&
    buffer[1] == 0xd8 &&
    buffer[2] == 0xff &&
    (buffer[3] & 0xf0) == 0xe0)

        char filename[7];    
        sprintf(*filename, "%03i.jpg", counter);
        FILE *output = fopen(filename, w);

    fwrite(&buffer, 512, 1, output);

You should use sprintf(filename, "%03i.jpg", counter); This is because name of the array represents it's first address which is needed in sprintf. Also, filename should be declared as char filename[8] You forgot to account for the \0 character which terminates all strings.

  • Thanks, so in what situation would I need a pointer for the first argument? – Haim Jun 19 '17 at 15:50
  • filename is a pointer. It is short for &filename[0] , i.e. name of array represents address of first memory location. To answer your question, you are using a pointer now and you need to always use it because the syntax demands it. – akshayk.vnit Jun 20 '17 at 16:53
  • sprintf(*filename, "%03i.jpg", counter), can anyone explain this line – Rohit Jain Jul 9 '19 at 7:44

You must log in to answer this question.

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