The CTBprintf
format objects allow to output formatted representations of elementary types on C++ streams with almost the same compactness as with the C call printf
but without loosing a type-safe interface. The format objects take care of all the stream setup, output the number, and restore the original stream state.
Why worry to learn yet another thing ?
Consider the following example: to output an integer in hexadecimal notation with a leading "0x", with 8 digits and zero-fill one just writes
cout << CTBprintf(iii,"$x0",10);
cout.setf(ios::showbase); cout.setf(ios::internal); cout << setfill('0') << setbase(16) << setw(10) << iii;
long flags = cout.flags(); char fill = cout.fill(); cout.setf(ios::showbase); cout.setf(ios::internal); cout << setfill('0') << setbase(16) << setw(10) << iii; cout.flags(flags); cout.fill(fill);
All variants of CTBprintf()
calls take at least three arguments:
[<modifiers>]<conversion_type>[<fill_character>]
+ | show + sign for positive numbers |
- | print field left justified |
. | always show decimal point |
$ | indicate base (precede with 0 or 0x for octal/hex) |
The conversion type should be one of
d | decimal conversion |
o | octal conversion |
x | hexadecimal conversion |
g | generic floating format |
f | fixed point floating format |
e | scientific floating format |
p | pointer format |
c | print int as char |
s | print string |
Any character after the conversion type is treated as fill character. If none is given blanks will be used.