sprintf style printing in C++

2

2b|!2b==?

I am not making much progress porting some old C code to C++ ...

I am porting the lines below, which prints an ISO8601 date string:

char buff[16] ;
memset(buff, '\0',16);
sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds) ;

to :

std::eek:stringstream os ;
os << std::setprecision(2) << hours << ":" << std::setprecision(2) <<
minutes << std::setprecision(2) << seconds ;


The C++ code compiles ok, but I am getting the wrong formatting (i.e.
for old time C programmers) - its as if I'm using the format string
"%2d:%2d%2d"

Can anyone spot where I'm going wrong ?
 
V

Victor Bazarov

2b|!2b==? said:
I am not making much progress porting some old C code to C++ ...

I am porting the lines below, which prints an ISO8601 date string:

char buff[16] ;
memset(buff, '\0',16);
sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds) ;

to :

std::eek:stringstream os ;
os << std::setprecision(2) << hours << ":" << std::setprecision(2) <<
minutes << std::setprecision(2) << seconds ;


The C++ code compiles ok, but I am getting the wrong formatting (i.e.
for old time C programmers) - its as if I'm using the format string
"%2d:%2d%2d"

Can anyone spot where I'm going wrong ?

You need to add setfill('0'), I believe.

V
 
S

Sylvester Hesp

2b|!2b==? said:
I am not making much progress porting some old C code to C++ ...

I am porting the lines below, which prints an ISO8601 date string:

char buff[16] ;
memset(buff, '\0',16);
sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds) ;

to :

std::eek:stringstream os ;
os << std::setprecision(2) << hours << ":" << std::setprecision(2) <<
minutes << std::setprecision(2) << seconds ;


The C++ code compiles ok, but I am getting the wrong formatting (i.e. for
old time C programmers) - its as if I'm using the format string
"%2d:%2d%2d"

Can anyone spot where I'm going wrong ?

You should use width instead of precision, and use a '0' fill character (see
std::setw and std::setfill in <iomanip>)

- Sylvester Hesp
 
D

David Harmon

On Mon, 23 Apr 2007 15:51:44 +0100 in comp.lang.c++, "2b|!2b==?"
os << std::setprecision(2) << hours << ":" << std::setprecision(2) <<
minutes << std::setprecision(2) << seconds ;

You need setfill('0') and width(2).
setprecision is for floating point.
 
J

James Kanze

I am not making much progress porting some old C code to C++ ...
I am porting the lines below, which prints an ISO8601 date string:
char buff[16] ;
memset(buff, '\0',16);
sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds) ;
to :
std::eek:stringstream os ;
os << std::setprecision(2) << hours << ":" << std::setprecision(2) <<
minutes << std::setprecision(2) << seconds ;
The C++ code compiles ok, but I am getting the wrong formatting (i.e. for
old time C programmers) - its as if I'm using the format string
"%2d:%2d%2d"
Can anyone spot where I'm going wrong ?
You should use width instead of precision, and use a '0' fill
character (see std::setw and std::setfill in <iomanip>)

Just for the record, in the more general case (outputting to a
file), you should save the previous fill character, and restore
it when you're through. (Obviously, it doesn't matter here,
since the stream is a local variable which will go immediately
out of scope when he's through.)

More generally, for time, I would not use ostringstream, but the
old C standby: strftime. It avoids most of the problems
inherent with sprintf, and it allows for localization when
appropriate.
 
M

Marcus Kwok

James Kanze said:
[snip]

More generally, for time, I would not use ostringstream, but the
old C standby: strftime. It avoids most of the problems
inherent with sprintf, and it allows for localization when
appropriate.

I would agree with James Kanze's advice to use strftime(). However, I
do not think localization is relevant in this particular instance since
the OP said he wanted to print an ISO8601 string, which AFAIU is
designed to eliminate localization issues :)

See:
http://www.cl.cam.ac.uk/~mgk25/iso-time.html

where he gives conversion specifiers for strftime() to output in
ISO8601 format.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top