How to use printf for CString??

I tried it like this, but it didn't work.

CString str = "abcde";
printf("str = %s\n", str.getBuffer());

-> output : a

printf("str = %s\n", (LPCTSTR)str);
-> output : a

I want to print all of characters the string has. How ?
I'm not sure the difference between CString and string or the difference between printf and cout but you could do something like this:
1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){
string str = "abcde";
cout << str << endl;
}


or

1
2
3
4
5
#include <iostream>
int main(){
std::string str="abcde";
std::cout << str << std::endl;
}
CString is from MFC, and both are different as I know.
Anyways, thanks
Back in the old days (using MFC's CString) you had to call the cast operator.
1
2
CString str = "some text";
printf("%s\n", const_cast<const char*>(str));
but more recent MFCs (as of VS 7.0), an ATL implementation is used. It happens to have the correct memory layout such that it works without the cast.
1
2
CString str = "some text";
printf("%s\n", str);

Thanks kbw. :)
Topic archived. No new replies allowed.