sprintf style printing in C++

Discussion in 'C++' started by 2b|!2b==?, Apr 23, 2007.

  1. 2b|!2b==?

    2b|!2b==? Guest

    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 ?
     
    2b|!2b==?, Apr 23, 2007
    #1
    1. Advertising

  2. 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::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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 23, 2007
    #2
    1. Advertising

  3. "2b|!2b==?" <> wrote in message
    news:D...
    >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
     
    Sylvester Hesp, Apr 23, 2007
    #3
  4. 2b|!2b==?

    David Harmon Guest

    On Mon, 23 Apr 2007 15:51:44 +0100 in comp.lang.c++, "2b|!2b==?"
    <> wrote,
    > 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.
     
    David Harmon, Apr 23, 2007
    #4
  5. 2b|!2b==?

    James Kanze Guest

    On Apr 23, 5:23 pm, "Sylvester Hesp" <> wrote:
    > "2b|!2b==?" <> wrote in message
    > news:D...


    > >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.

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Apr 24, 2007
    #5
  6. 2b|!2b==?

    Marcus Kwok Guest

    James Kanze <> wrote:
    >> "2b|!2b==?" <> wrote in message
    >> news:D...
    >> > 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
     
    Marcus Kwok, Apr 26, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,514
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    514
    msalters
    Dec 9, 2004
  3. Ken Varn
    Replies:
    0
    Views:
    473
    Ken Varn
    Apr 26, 2004
  4. iffy agbim
    Replies:
    1
    Views:
    110
    Mark Andrews
    May 24, 2004
  5. iffy agbim
    Replies:
    0
    Views:
    105
    iffy agbim
    May 21, 2004
Loading...

Share This Page