Skip to main content
gamedev.net
🔒 Locked

std::string formating

Started by Mihail121 16 replies 1.6k views
Original Post
Mihail121
Mihail121
Hi! Normaly with C++ i can do this: char str[64]; sprintf(str,"%d %d %d",10,20,30); now can i do the same thing with the std::string.I mean i know about: str << 10 << " " << 20 << " " << 30; but there must be some easier way!10x! http://mihail121.tripod.com
Fruny
Fruny
Formatting with iostream is ... painful.
I can only refer you to your C++ manual.

Or try boost::format


[
Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
noVum
noVum
That''s the easy way

why is:

sprintf(str,"%d %d %d",10,20,30);

easier than:

str << 10 << " " << 20 << " " << 30;

O_o
MauMan
MauMan
stream formatting in C++ makes baby Jesus cry...
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
MauMan
MauMan
Seriously, it's a trade-off.

The C++ stream way gets you type safety and you don't have to know the length of buffer before-hand. However, it's less efficient (generally more function calls) and if you want to do custom formating it's a pain.

The C way offers easier custom formatting and may be more efficient but it's not type safe and you have to know the size of the buffer before-hand.


[edited by - mauman on September 4, 2003 10:21:22 AM]
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Fruny
Fruny

#include <boost/format.hpp>

// Unlike printf, the format string is only parsed *once*

boost::format fmt( "%d %d %d\n" );

std::cout << fmt % 10 % 20 % 30;
std::cout << fmt % 100 % 5 % 72;



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on September 4, 2003 10:22:16 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Lord Bart
Lord Bart
Hello Mihail121,

you have to do alot more typing
setprecision( ) how many decimal places, setw( ) how many chars wide
hex, dec, oct output in these units.

float t = 4506.56034;

ie cerr << setprecision(2) << t << endl
procduces 4506.56

I real wish when they made te stream class they use the % format for doing this.
So we could do cerr << %.2 << t << endl;

But in there wisdom I guess they just wanted it to be a pain in the butt to use.
Most of our code at work if the is a lot of formatting we use printf and then << it to a stream.

Lord Bart
MauMan
MauMan
I have not used boost::format. Is it type-safe? Would

std::cout << boost::format( "%d %lf %d\n" ) % 10 % 20 % 30;

blow it up since 20 is not a double?
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Fruny
Fruny
quote:
Original post by MauMan
I have not used boost::format. Is it type-safe? Would

std::cout << boost::format( "%d %lf %d\n" ) % 10 % 20 % 30;

blow it up since 20 is not a double?


No, it won''t blow up. boost::format is type-safe. It really uses the appropriate << operator - the format string is only used, well, for formatting ( number of decimal places, hex vs decimal ... )


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
MauMan
MauMan
quote:
Original post by Fruny
quote:
Original post by MauMan
I have not used boost::format. Is it type-safe? Would

std::cout << boost::format( "%d %lf %d\n" ) % 10 % 20 % 30;

blow it up since 20 is not a double?


No, it won't blow up. boost::format is type-safe. It really uses the appropriate << operator - the format string is only used, well, for formatting ( number of decimal places, hex vs decimal ... )



Fruny, not to be too forward but you are my new best friend ;-)

[edited by - mauman on September 4, 2003 10:28:00 AM]
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Fruny
Fruny
quote:
Original post by MauMan
Fruny, not to be too froward but you are my new best friend ;-)


Err, thanks... I guess ...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Mihail121
Mihail121
I must say that the thing have gone a little over...i mean:

void PrintMyDamnMsg(const char *msg,...)
{
}

i''ll be forced to use char pointer right?Exept if i don''t parse the parameter list my self and try to think of an impossible way to link with boost::format or something like that...

http://mihail121.tripod.com

Fruny
Fruny
Hmm. Ok, I think I misread your question.
You want to output your message to a string ?

Something like

#include <string>
#include <sstream>

int main()
{
std::ostringstream oss;
oss << 10 << ' ' << 20 << ' ' << 30;
std::string str = oss.str();

// And cleanup - although it is not necessary in this example

oss.str(""); // reset the string in the string stream

oss.clear(); // clear any error flags that have been set.

}
?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on September 4, 2003 11:17:25 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Mihail121
Mihail121
yeah that kinda stuff.say we have:



void format_msg(char *dst,const char *src,...)
{
va_list arg_list;

va_start(arg_list,src);
vsprintf(dst,src,arg_list);
va_end(arg_list);
}



now i wanna change that with the STL string but how.The only chance i see is:



void format_msg(std::string dst,const char *src,...)
{
char str[512];
va_list arg_list;

va_start(arg_list,src);
vsprintf(str,src,arg_list);
va_end(arg_list);

dst = str;
}



and you understand that this is a little inefficient!

http://mihail121.tripod.com

Martel
Martel
You want either std::strstream or std::stringstream... I can never remember the difference.

Whichever it is... is just an iostream that writes to a memory buffer. Then you can get the string value from the buffer. It''s not exactly efficient but you can use ios inserters and all that painful crap as normal, and it will be nice and typesafe and memory safe.

JohnBolton
JohnBolton
quote:
Original post by Mihail121
void format_msg(char *dst,const char *src,...)
{
...
vsprintf(dst,src,arg_list);
...
}
...
char str[512];
...
vsprintf(str,src,arg_list);
...




This is the kind of code that hackers dream about.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.