valentin
valentin

Reputation: 1123

Sequence of chars with numerical parts

I am using something like sprintf(base_name,"%d.pgm", frame_number); and passing frame_number as an integer (i.e. 1, 2, 3, ...10, 11 etc.) to generate the following sequence of image names:

1.pgm, 2.pgm, 3.pgm....10.pgm, 11.pgm etc.

How can I change this to output the following:

0001.pgm, 0002.pgm, 0003.pgm....0010.pgm, 0011.pgm etc.

I am using this to load images from a file.

Upvotes: 0

Views: 59

Answers (2)

nhgrif
nhgrif

Reputation: 62062

sprintf(base_name, "%04d.pgm", frame_number); I believe is what you're looking for.

%04d means...

%d is a place holder for an int. 0 indicates leading zeros. 4 indicates total field width minimum of 4 characters wide (and in this case, extra spaces are filled with your leading zeros).

These are called format specifiers and they come from C, so they're universal across all C-based languages as far as I know. If you have questions similar to this in the future, just search for C printf format specifiers or something, and you'll probably get what you're looking for.

Upvotes: 3

Sharon Dorot
Sharon Dorot

Reputation: 542

Use %04d.

0 means add 0 at the start, 4 .means how many min digits

Upvotes: 3

Related Questions