0

I have been on recover for a while now,and I am recieving an error when compiling the code:

recover.c:31:42: error: data argument not used by format string [-Werror,-Wformat-extra-args] sprintf(filename, "03i.jpg", counter);

I am struggling with the error and this problem in general, so any help would be greatly appreciated, here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//define variables
const int BLOCK_SIZE = 512;
typedef uint8_t BYTE;
int counter = 0;
char filename[8];

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

    // initislize variables
    BYTE buffer[BLOCK_SIZE];

     //open memory card
    FILE *f = fopen (argv[1], "r");
    //read file
    while(fread(&buffer,  1, BLOCK_SIZE, f)==BLOCK_SIZE)
    {
           //check 4 headers for jpeg
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //make new JPEG file
            sprintf(filename, "03i.jpg", counter);
            //start writing to new file
           FILE *img = fopen(filename, "w");
              // incriment counter
              counter ++;
                 //write data to file
                 fwrite(&buffer, 1, BLOCK_SIZE, img);
         }
         else if(f != NULL)
         {
            fwrite(&buffer, 1, BLOCK_SIZE, f);
         }
     }
    //close files
     fclose(f);
    //return 0
     return 0;
  }

1 Answer 1

0

It's failing because there's no formatting in the string. Look carefully:

        sprintf(filename, "03i.jpg", counter);

The 03i should be preceeded with a % sign to indicate formatting. Without it, the compiler sees "03i.jpg" as a literal string to print with no formatting or variable placeholders and "counter" as a parameter that can't be applied to anything.

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

You must log in to answer this question.

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