Free Source Code and Program Tips
How to translate among CString,string and char array
In MFC program, you’d better try to avoid using std::string. There is no compelling reason to use it in MFC, and it leads to confusion.
There are some standard method to translate among Cstring ,string and char[].
// Suppose
CString str;
std::string s;
char buf[SIZE];
// from CString to std::string
s = str;
// from std::string to CString
str = s.c_str();
// to char[]
_tcscpy(buf, str);
_tcscpy(buf, s.c_str());
// from char[]
str = buf;
s = buf;
Also, beware of unicode. If UNICODE is defined, you should convert between unicode and ansi versions. Better to use TCHAR instead of char and std::basic_string
| Print article | This entry was posted by support on June 22, 2007 at 11:56 am, and is filed under General. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |