0

I wrote a perfectly functional recover.c program yesterday.

But I can't get the file naming right. How can I add leading zeroes to the file name so the name is 000 instead of just 0?

1
  • Please consider leaving this question open if my edit is accepted because it will make sense, it has a good answer, and might help others.
    – Chrisuu
    Commented Jan 7, 2015 at 17:49

1 Answer 1

3

To print 000 you have to specify the format in the place holder.

for printing should be something like this:

printf("%03d", 0);

but to create a file with the name 000.jpg i think you need to use the function sprintf() wich can convert an integer to a string and then use this string to create the file.

sprintf(filename, "%03d.jpg", jpg_counter);

In this case the fist argument, filename, is where the resulting string is stored, you need to declare this string first. If the variable jpg_counter is 0 the resulting string in this case is filename = "000.jpg" the second argument is what you want to convert to a string. the third is tha variable you want to use for the placeholder.

You must log in to answer this question.

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