5

What I'm trying to is to write something like this

file << someFunction(5) << hex << 3;

And to get an output like this 00003 instead of 3 (and 300 will be 00300, etc)

Note:- Sorry if this was asked before, I don't know what term to search for to get this

EDIT:- I mean the zero's to the left, i included the hex part just to be clear I want it to be compatible with this format

4

3 回答 3

12

我相信这就是你的意思。

#include <iostream>
#include <iomanip>
int main()
{
   std::cout << std::setw(5) << std::setfill('0') << 3 << '\n';
   return 0;
}

输出

00003

链接 setwsetfill

编辑:另外,请参阅这个问题std::internal

于 2013-05-09T14:02:41.237 回答
4

利用

file << setfill('0') << setw(5) << 3;

得到00003而不是3

于 2013-05-09T14:03:14.120 回答
2

iomanip 是您需要搜索的内容。

#include <iomanip>

然后

file << someFunction(5) << hex << setw(5) << setfill('0') << 3 << endl;
于 2013-05-09T14:05:29.910 回答