🔒 Locked
std::string formating
Advertisement
Original Post
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
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 ]
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 ]
quote:
Original post by noVum
That''s the easy way
why is:
sprintf(str,"%d %d %d",10,20,30);
easier than:
str << 10 << " " << 20 << " " << 30;
O_o
Cause it will be performed in _one_ operation and not in 10!
http://mihail121.tripod.com
Well it''s my first day using ofstream,ifstream,string and all other stuff so i though it can be done but if it can...oh well!
http://mihail121.tripod.com
http://mihail121.tripod.com
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]
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]
#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]
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
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
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 ]
quote:
Original post by Frunyquote:
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]
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 ]
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
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
Hmm. Ok, I think I misread your question.
You want to output your message to a string ?
Something like
[ 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]
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]
yeah that kinda stuff.say we have:
now i wanna change that with the STL string but how.The only chance i see is:
and you understand that this is a little inefficient!
http://mihail121.tripod.com
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
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.
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.
quote:
Original post by Mihail121void 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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement