I want to justify the output text like this
0x29823d80 0x10019b8 / 0 00000000000000000000000000000001
0x37449e60 0x10dfc / 12 00000000000000000001000000000000
However with this statement
fout << std::setw(5) << std::hex << "0x" << (*it).addr << " "
<< std::setw(5) << std::hex << "0x" << (*it).pc << std::setw(10) << "/" << std::setw(5) << std::dec << (*it).off << "\t"
<< std::setw(5) << (*it).layout << "\n";
I get this:
0x29823d80 0x10019b8 / 0 00000000000000000000000000000001
0x37449e60 0x10dfc / 12 00000000000000000001000000000000
From this reference:
This means that the
setw
you do is for the string"0x"
and not the actual hex number. You have to usesetw
right before the output you want to justify.Edit: One solution to your problem is to use temporary string containing the leading
"0x"
, and the format with those string instead:The expression
10 + 10 - val2.str().length()
above I get from this:10
because you seem to want 10 spaces between the number and the slash10
because that allows for 8 hex digits (32 bits) plus the leading"0x"
You can see an example using this method here.
I know this may not be what you are after, but remember that
C
is mostly a subset ofC++
, and that you in particular have the function fprintf available, which is much more suited for formatting simple strings and numbers than theC++
I/O facilities. With this, you would simply write:When in doubt, use more
setw
! Also, you can usesetfill
to make the numbers look prettier:Produces: