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

HElwy
  • 81
  • 1
  • 6
  • Do you mean hexadecimal representation, or do you mean padding with zero on the left side? The code seems to point to the former, but the example of input and output seems to point to the latter. – Aleph May 09 '13 at 13:59
  • Is an error or warning spitted out? – PurityLake May 09 '13 at 14:00

3 Answers3

12

I believe this is what you mean.

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

Output

00003

Links setw, setfill.

Edit: Also, see std::internal at this question.

Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
4

Use

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

to get 00003 instead of 3

Oszkar
  • 687
  • 1
  • 5
  • 20
2

iomanip is what you need to search for.

#include <iomanip>

then

file << someFunction(5) << hex << setw(5) << setfill('0') << 3 << endl;
parkydr
  • 7,596
  • 3
  • 32
  • 42