Print numbers sequentially with printf with padding zeros

in C ++ using printf I want to print a sequence of numbers, so I get from the "for" loop,

1
2
...
9
10
11

and I create files from these numbers. But when I list them using "ls", I get

10
11
1
2
..

so instead of trying to solve the problem with bash, I wonder how I could print;

0001
0002
...
0009
0010
0011

etc.

thank

+3
source share
8 answers
i = 45;
printf("%04i", i);

=>

0045

Basically, 0 points printf to "0", 4 to a digit, and "i" is a placeholder for an integer (you can also use "d").

See Wikipedia for format entries.

+10
source

printf("%04d\n", Num); . printf.

+5
printf("%04d", n);
+4

++, printf()?

cout .

 #include <iostream>
 #include <iomanip>

 using namespace std;

 int main(int argc, char *argv[])
 {

    for(int i=0; i < 15; i++)
    {
        cout << setfill('0') << setw(4) << i << endl;
    }
    return 0;
 }

:


0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014

++ !

+3
printf("%4.4d\n", num);
+1

,

" "

0

% 04d .

0

:

for(int i = 0; i != 13; ++i)
  printf("%*d", 2, i)

"% * d"? ; . IOStreams setw(int)

0

Source: https://habr.com/ru/post/1749054/


All Articles