Little help on stream...

  • Thread starter Mathieu Malaterre
  • Start date
M

Mathieu Malaterre

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::eek:stringstream 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
 
V

Victor Bazarov

Mathieu Malaterre said:
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::eek:stringstream 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::eek:stringstream 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
 
J

John Harrison

Victor Bazarov said:
#include <sstream>
#include <string>
#include <iomanip>

int main ()
{
int group = 10, element = 20;
std::eek:stringstream 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
 
M

Mathieu Malaterre

Victor said:
#include <sstream>
#include <string>
#include <iomanip>

int main ()
{
int group = 10, element = 20;
std::eek:stringstream 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...
 
R

red floyd

John said:
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.
 
J

John Harrison

red floyd said:
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
 
J

John Harrison

Mathieu Malaterre said:
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
 
M

Michael Kurz

Mathieu Malaterre said:
Victor Bazarov wrote:
}

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

John Harrison

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
 
M

Mathieu Malaterre

Michael said:
Victor Bazarov wrote:
}

entire

easier


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
 
J

John Harrison

<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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top