473,385 Members | 1,356 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,385 software developers and data experts.

Little help on stream...

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
10 2873
"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

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

"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

"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

"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
>
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
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
>>
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
2
by: Sava Mikalački | last post by:
Hi to all of you god programmers in the world! One question: When I want to load a value in some variable (ex. int x) I use cin << x. Ok, but if I want to use cin.get() what would it be? I'we...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
8
by: Brent White | last post by:
If you need more information, I'd be glad to give it, but I am developing an ASPX page so that the user can select multiple values for a specific field, then pass them on (using Crystal Reports 10...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
2
by: camainc | last post by:
I'm just now migrating to the 2.0 framework, and in working with the System.Xml namespace, it seems like almost every method or object either takes a URI or a stream or some other object that only...
0
by: shapper | last post by:
Hello, I followed some examples in internet and in MSDN web site to create a serializable class. Basically, I need to serialize a class to binary so I can save it in an SQL 2005 table using a...
0
by: moltendorf | last post by:
Alright, I finally worked up a massive SQL query that gets me the result I want for my PHP 6 CMS: Module Engine Class. I have read various comments within the dev.mysql.com documentation of MySQL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.