在C++中,使用printf我想打印一个数字序列,所以我得到了一个"for"循环;
1
2
...
9
10
11
Run Code Online (Sandbox Code Playgroud)
我从这些数字创建文件.但是当我用"ls"列出它们时,我得到了
10
11
1
2
..
Run Code Online (Sandbox Code Playgroud)
因此,我不知道如何使用bash来解决问题,我不知道如何打印;
0001
0002
...
0009
0010
0011
Run Code Online (Sandbox Code Playgroud)
等等
谢谢
sch*_*der 10
i = 45;
printf("%04i", i);
Run Code Online (Sandbox Code Playgroud)
=>
0045
Run Code Online (Sandbox Code Playgroud)
基本上,0告诉printf填充'0',4是数字计数,'i'是整数的占位符(你也可以使用'd').
有关格式占位符,请参阅Wikipedia.
如果您使用的是 C++,那么您为什么使用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;
}
Run Code Online (Sandbox Code Playgroud)
这就是您的输出的外观:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
Run Code Online (Sandbox Code Playgroud)
C++ 来救援!