Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to CString::Format?

Is there any better alternative for doing string formatting in VC6, with syntax checking before substitution?

like image 966
yesraaj Avatar asked Sep 21 '26 09:09

yesraaj


1 Answers

CString offers the Format method for printf-style formatting, but this isn't type-safe.

For type-safe string formatting you could either use std::stringstream / std::wstringstream or the Boost Format library, although these both work with the C++ std::basic_string class template, and not the MFC CString class. I've used both of these successfully in VC6.

Boost Format is nice because it allows you to use printf-like syntax, and will throw an exception if the arguments you supply don't match the format string, whereas string formatting with C++ iostreams tends to make your code quite verbose.

Note that you can create a CString object from a std::string as follows:

std::string s;
CString str( s.c_str() );

I hope this helps!

like image 200
ChrisN Avatar answered Sep 23 '26 03:09

ChrisN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!