Re: Little help on stream...
From: Mathieu Malaterre (malatNOSP_at_Mfree.fr)
Date: 10/08/04
- Next message: John Alway: "C++ Classes within DLLs?"
- Previous message: John Harrison: "Re: Little help on stream..."
- In reply to: Victor Bazarov: "Re: Little help on stream..."
- Next in thread: John Harrison: "Re: Little help on stream..."
- Reply: John Harrison: "Re: Little help on stream..."
- Reply: Michael Kurz: "Re: Little help on stream..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 08 Oct 2004 05:02:40 GMT
Victor Bazarov wrote:
> "Mathieu Malaterre" <malatNOSP@Mfree.fr> wrote...
>
>>I am trying to get rid a of sprintf in a c++ code, but I tried in several
>>ways and couldn't figure out how to change:
>>
>>uint16_t group, uint16_t element;
>>sprintf(buffer, "%04x|%04x", group , element);
>>
>>so far I have something like:
>>
>>std::ostringstream buf;
>>buf.flags ( std::ios_base::right |std::ios_base::hex );
>>buf.width( 4 );
>>buf << group;
>>buf << "|";
>>buf << std::hex << element;
>>std::string key = buf.str();
>>
>>But doesn't seems to work...
>
>
> #include <sstream>
> #include <string>
> #include <iomanip>
>
> int main ()
> {
> int group = 10, element = 20;
> std::ostringstream buf;
> buf << std::right << std::setw(4) << std::setfill('0') << std::hex <<
> group
> << "|"
> << std::right << std::setw(4) << std::setfill('0') << std::hex <<
> element;
> std::string key = buf.str();
>
> return 0;
> }
>
> And, just between us, you don't have to get rid of 'sprintf'. The entire
> C Standard Library is part of C++ Standard Library. If something's easier
> to do with a C function, by all means, do it.
Yes I know this is true on linux. But I have had so many weird random
bugs on Windows when I mixed old cstring and new string that now I
really try to choose one OR the other...
Thanks anyway you saved me a *lot* of time...
Mathieu
Ps: Of course I'd appreciate any comment if somebody know what is going
on. I suspect it has to do with 'cout' or 'cerr' that coud be declared
as static...
- Next message: John Alway: "C++ Classes within DLLs?"
- Previous message: John Harrison: "Re: Little help on stream..."
- In reply to: Victor Bazarov: "Re: Little help on stream..."
- Next in thread: John Harrison: "Re: Little help on stream..."
- Reply: John Harrison: "Re: Little help on stream..."
- Reply: Michael Kurz: "Re: Little help on stream..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|