I have the following code (simplified):
ostringstream oss;
oss << "Text ";
oss << hex << uppercase;
oss.width(8);
oss.fill('0');
oss << var1 << " ";
oss << var2 << " ";
oss << dec << nouppercase;
oss.width(1);
oss << var3 << " another text." << endl;
string result = oss.str();
// work with result...
Where var1
, var2
are unsigned int
's and var3
is an int
. The idea is to create a string where var1
and var2
are formated like a hex number (but without the 0x
) and var3
as a regular integer. What i found out that only the first number is correctly formated, the second one is not padded with zeroes:
Text 000AF00C 3B7FF 1 another text.
After a while i found out that setting the width and fill parameters AGAIN fixes this. Is there a way how to avoid specifying these formating rules over and over again for each number? The amount of formatted variables is much higher than 2, this was just a simplified example. Wrapping all this into a function is an option, but i would really like to learn how to preserve the formatting with ostringstream
.