Re: arbitrary number of digits at integer representation
ding ding wrote:
yinyang wrote:
some silly question, but I cannot come across the method:
how to push an integer to text stream with arbitrary N digits, that is 5 ->
0005 for N=4 and so on..
there are two method.
Not really.
C format:
int Number=8;
char strTemp[64];
memset(strTemp,0,64);
sprintf(strTemp,"%05d",Number);
printf("%s\n",strTemp);
C format:
std::string
cFormat(
int value,
int width,
int prec )
{
static char const fmt[] = "%*.*d" ;
int l = snprintf( NULL, 0, fmt, width, prec,
value ) ;
assert( l >= 0 ) ;
std::vector< char > buffer( l + 1 ) ;
snprintf( &buffer[ 0 ], buffer.size(), fmt, width, prec, value
) ;
return std::string( buffer.begin(), buffer.end() ) ;
}
All your code does is guarantee the minimum width, using a fill
of '0'. And of course, with no guarantee that the code won't
run into undefined behavior on certain systems. (It is almost
impossible to use sprintf correctly; if you insist on using the
C routines, at least use snprintf.)
C++ format:
#include <iostream>
using namespace std;
int Number=8;
cout.width(5);
cout.fill( '0');
cout << Number << endl;
std::string
cppFormat(
int value,
int width,
int prec )
{
std::ostringstream tmp ;
tmp << std::setfill( '0' ) << std::setw( prec ) << value ;
std::ostringstream result ;
result << std::setw( width ) << tmp.str() ;
return result.str() ;
}
Note too that it takes two separate ostringstream's to do this.
For some reason (probably because the effect is sticky),
precision is ignored when outputting integers.
Of course, code like this should never be used on anything but a
local ostringstream -- you should always restore the formatting
parameters to their initial value. And of course, if someone
else has previously output in hex, and forgotten to restore it,
your output would be in hex. The standard procedure here would
be to write a specific manipulator for the logical element, and
not to specify the physical formatting individually for each
output. Such manipulators will typically set -- and restore --
everything, so you are protected against someone accidentally
forgetting a restore, and you don't interfere with someone who
accidentally forgot to set everything, and is counting on the
default values.
BTW: your code doesn't compile with my compiler. Putting the
code parts in main, I get:
padx.cc: In function 'int main()':
padx.cc:9: error: invalid use of undefined type 'struct
std::ostream'
/home/team02/jakan/gnu/gcc/install-4.0.2/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/iosfwd:64:
error: declaration of 'struct std::ostream'
padx.cc:10: error: invalid use of undefined type 'struct
std::ostream'
/home/team02/jakan/gnu/gcc/install-4.0.2/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/iosfwd:64:
error: declaration of 'struct std::ostream'
padx.cc:11: error: no match for 'operator<<' in 'std::cout <<
Number'
padx.cc:11: error: 'endl' was not declared in this scope
I have to include at least <ostream> as well. (Of course, I've
modified the library of my compiler expressedly to catch this
error. I'd be surprised if you had problems with any "normal"
compiler.)
(For my example, I need to include <ostream>, <sstream>, <ios>
and <iomanip>. In theory, at least -- I don't think it's
possible for <ostream> not to include <ios>, for example, even
though the standard theoretically allows it, and I suspect that
you won't find a compiler where <sstream> and <iomanip> don't
suffice.)
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]