Is there any better alternative for doing string formatting in VC6, with syntax checking before substitution?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With