473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sprintf style printing in C++

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::ostringstream 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 ?
Apr 23 '07 #1
5 4431
2b|!2b==? wrote:
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::ostringstream 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #2

"2b|!2b==?" <ro**@your.box.comwrote in message
news:Dc******************************@bt.com...
>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::ostringstream 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
Apr 23 '07 #3
On Mon, 23 Apr 2007 15:51:44 +0100 in comp.lang.c++, "2b|!2b==?"
<ro**@your.box.comwrote,
> 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.

Apr 23 '07 #4
On Apr 23, 5:23 pm, "Sylvester Hesp" <s.h...@oisyn.nlwrote:
"2b|!2b==?" <r...@your.box.comwrote in message
news:Dc******************************@bt.com...
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::ostringstream 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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Apr 24 '07 #5
James Kanze <ja*********@gmail.comwrote:
>"2b|!2b==?" <r...@your.box.comwrote in message
news:Dc******************************@bt.com...
I am porting the lines below, which prints an ISO8601 date string:
[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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Venkat Venkataraju | last post by:
Hi All I'm having problems printing a string with a % char. Here is the code line = sprintf("<table width=100% id=%s>\n", $table_id); I'm getting an not enough parameter error. The % for the...
2
by: Huey | last post by:
Hi All, I saved hashed values of hex into buf, then buf becomes unreadable by the means of calling puts(buf), printf("%s") and so on. Then, I need to convert the hex in buf into a char buffer...
32
by: Michele | last post by:
I have a code line like this: sprintf(buf, "%20g",*(liftPtr++)) what does it mean? And what does the function sprintf do? Thanks michele
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
2
by: Lars Eighner | last post by:
I notice that many examples in the manual use sprintf in constructing database query strings. Is this just style, or are there some serious advantages to sprintf over concatenating the string,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.