By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

Little help on stream...

P: n/a
Hello,

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

Thanks for any help/pointer
Mathieu
Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Mathieu Malaterre" <ma*******@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.

V
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TYo9d.142704$wV.42458@attbi_s54...
"Mathieu Malaterre" <ma*******@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;
}


Setw must be repeated but setfill and hex do not, and right is the default.

buf << std::setfill('0') << std::hex << std::setw(4) << group << "|"
std::setw(4) << element;

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.


Absolutely.

john
Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
"Mathieu Malaterre" <ma*******@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...
Jul 22 '05 #4

P: n/a
John Harrison wrote:

Setw must be repeated but setfill and hex do not, and right is the default.


Anyone know the committee's rationale behind that one? It's very
confusing when dealing with inconsistent output formatting, and one of
the things that makes me still use printf() -- I *KNOW* how to make it
do what I want. I've tried with iostreams, but it's just a pain in the
ass to get properly formatted output.
Jul 22 '05 #5

P: n/a

"red floyd" <no*****@here.dude> wrote in message
news:jq**************@newssvr14.news.prodigy.com.. .
John Harrison wrote:

Setw must be repeated but setfill and hex do not, and right is the
default.

Anyone know the committee's rationale behind that one?


It would probably be worse if setw didn't have to be repeated, consider

cout << setw(4) << x << ',' << y << ',' << z << '\n';

The width of four would not just apply to x, y and z, but also to the commas
and newline.
It's very confusing when dealing with inconsistent output formatting, and
one of the things that makes me still use printf() -- I *KNOW* how to make
it do what I want. I've tried with iostreams, but it's just a pain in the
ass to get properly formatted output.


Perhaps you should look at the format library from boost. Never used it
myself but it gives printf style formatting for C++ streams.

john
Jul 22 '05 #6

P: n/a

"Mathieu Malaterre" <ma*******@Mfree.fr> wrote in message
news:Qb********************@twister.nyroc.rr.com.. .
Victor Bazarov wrote:
"Mathieu Malaterre" <ma*******@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...


I've never had any problems like that with Windows and I've used several
different versions of the dinkumware library that's supplied with most
Windows compilers. They do have a bug list for VC++ 6,
http://www.dinkumware.com/vc_fixes.html, perhaps you should check it out.

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


That comment makes no sense to me. How can you declare cout or cerr as
anything? They are declared in the header file iostream. In any case it
would help us help you if you explained what went wrong with your own code,
to me it looks like you used width incorrectly.

john
Jul 22 '05 #7

P: n/a

"Mathieu Malaterre" <ma*******@Mfree.fr> schrieb im Newsbeitrag
news:Qb********************@twister.nyroc.rr.com.. .
Victor Bazarov wrote:
}

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


I used the following wrapper code to create formated strings on windwows
(VC7, VC6) as well as on linux (gcc 3.3.4) and I never experienced any
problems with it.
Regards
Michael

<snip>
#include <stdarg.h>
#include <stdio.h>

std::string format(const char* format, ...){
char buffer[2048];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
return buffer;
}
</snip>
Jul 22 '05 #8

P: n/a
>
I used the following wrapper code to create formated strings on windwows
(VC7, VC6) as well as on linux (gcc 3.3.4) and I never experienced any
problems with it.
Regards
Michael

<snip>
#include <stdarg.h>
#include <stdio.h>

std::string format(const char* format, ...){
char buffer[2048];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
return buffer;
}
</snip>


Ouch! Ever heard of buffer overruns? This is a security flaw waiting to be
exposed. If you possibly can replace vsprintf with the C99 standard
vsnprintf, or the MS function _vsnprintf.

John
Jul 22 '05 #9

P: n/a
Michael Kurz wrote:
"Mathieu Malaterre" <ma*******@Mfree.fr> schrieb im Newsbeitrag
news:Qb********************@twister.nyroc.rr.com.. .
Victor Bazarov wrote:
}
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...

I used the following wrapper code to create formated strings on windwows
(VC7, VC6) as well as on linux (gcc 3.3.4) and I never experienced any
problems with it.
Regards
Michael

<snip>
#include <stdarg.h>
#include <stdio.h>

std::string format(const char* format, ...){
char buffer[2048];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
return buffer;
}
</snip>


This solution is even better. Thanks a lot

Mathieu
Jul 22 '05 #10

P: n/a
>>
<snip>
#include <stdarg.h>
#include <stdio.h>

std::string format(const char* format, ...){
char buffer[2048];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
return buffer;
}
</snip>


You should apply for a job at Redmond.

john
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.