Zhu051200
asked on
convert CSting to LPTSTR
In using a ListCtrl, the lvItem.pszText must be a type of LPTSTR. I get a CString from another ListCtrl( m_FileList.GetItemText(i,0 )) .Then how to give the CString to the lvItem.pszText?
I searched the PAQ and tried several ways like these:
lvItem.pszText = (LPTSTR) m_FileList.GetItemText(i,0 ).GetBuffe r(0);
strcpy(lvItem.pszText, m_FileList.GetItemText(i,0 ))
but they all can't work. I am really confused. Who can tell me the difference between CString, string, char *str[], LPTSTR....? How to make this work?
I searched the PAQ and tried several ways like these:
lvItem.pszText = (LPTSTR) m_FileList.GetItemText(i,0
strcpy(lvItem.pszText, m_FileList.GetItemText(i,0
but they all can't work. I am really confused. Who can tell me the difference between CString, string, char *str[], LPTSTR....? How to make this work?
You can obtain a "LPSTR" or "char *" string pointer from a a "CString" or a "string" object. for a "CString" you just use the "LPSTR" conversion operator (which is usually invoked automatically as needed like
CString S = "ABC";
LPSTR P = S; // P now points to the string stored by S;
For a "string" you must do this using the c_str() member function like
string S = "ABC";
char *P = S.c_str(); // P now points to the string stored by S;
Now, there is a complication. The pointers returned in this way are not guaranteed to be valid for ever. The string objects (S) may change the location used to store their strings. If they do so, then the pointer to the string that you previously obtained will no longer be valid. You are guaranteed that the poiners obtained in this way are valid as long as no non-const member functions are called on the string object. So, for example
CString S = "ABC";
LPSTR P = S; // P now points to the string stored by S;
S.GetLength(); // Const function.
char C1 = S.GetAt(0); // Const function.
// P is still valid.
S = "ABCDEF"; // S is changed.
// P may not be valid now.
CString S = "ABC";
LPSTR P = S; // P now points to the string stored by S;
For a "string" you must do this using the c_str() member function like
string S = "ABC";
char *P = S.c_str(); // P now points to the string stored by S;
Now, there is a complication. The pointers returned in this way are not guaranteed to be valid for ever. The string objects (S) may change the location used to store their strings. If they do so, then the pointer to the string that you previously obtained will no longer be valid. You are guaranteed that the poiners obtained in this way are valid as long as no non-const member functions are called on the string object. So, for example
CString S = "ABC";
LPSTR P = S; // P now points to the string stored by S;
S.GetLength(); // Const function.
char C1 = S.GetAt(0); // Const function.
// P is still valid.
S = "ABCDEF"; // S is changed.
// P may not be valid now.
ASKER
sorry, nietod, your comment makes me learn a lot. But when I use this:
CSting S = "ABC";
LPSTR P = S;
The compiler tells:
cannot convert from 'class CString'
to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Did I miss something?
CSting S = "ABC";
LPSTR P = S;
The compiler tells:
cannot convert from 'class CString'
to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Did I miss something?
Hi,
Try to
CSting S = "ABC";
LPSTR P = (LPSTR)(LPCTSTR)S;
Hope this helps,
mahno
Try to
CSting S = "ABC";
LPSTR P = (LPSTR)(LPCTSTR)S;
Hope this helps,
mahno
I see. The CString class does not convert to a "LPSTR", which is basically "char *", it converts to a "LPCSTR" (Note the "C"), which is basically a "const char *". Try
CSting S = "ABC";
LPCSTR P1 = S;
const char *P2 = S;;
Note that it is not necessary to reject an answer if you only want additional information or clarification. Most questions are answered via a dialog between an expert and a client. It is not neccessary to reject an answer as long as the dialog is progressing.
CSting S = "ABC";
LPCSTR P1 = S;
const char *P2 = S;;
Note that it is not necessary to reject an answer if you only want additional information or clarification. Most questions are answered via a dialog between an expert and a client. It is not neccessary to reject an answer as long as the dialog is progressing.
manho, did you read the question history? Do you understand that my answer was rejected unnecessarily? Also do you understand the implications of your proposed solution?
ASKER
I am very very sorry, nietod. I am a beginner, so I did not know clearly about how experts-exchange works. I won't do so unnecessarily later.
CString can covert to "const char *" but then how to convert it to "char *"? I found a way that uses strcpy. Like this:
char * s[80];
CString str = "ABC";
strcpy(s, str);
I had to assign the size of s, or it will run error. Because the CString doesn't have a certain size ,I hope s can change it's size as needed. How to implement this? Are there any other ways?
I have tried mahno's suggest. It can compile, but can't get the right result.
CString can covert to "const char *" but then how to convert it to "char *"? I found a way that uses strcpy. Like this:
char * s[80];
CString str = "ABC";
strcpy(s, str);
I had to assign the size of s, or it will run error. Because the CString doesn't have a certain size ,I hope s can change it's size as needed. How to implement this? Are there any other ways?
I have tried mahno's suggest. It can compile, but can't get the right result.
Use CString as a LPSTR...
char * s[80]; <----what's this?
CString str = "ABC";
strcpy(s, str);
->CString can covert to "const char *" but then how to convert it to "char *"? I found a way that uses strcpy. Like this:
========================== ==
You can simply convert it using C conversion ,(LPSTR)...or const_cast...
mahno , you'd not answer this repeatedly when the right answer was rejected unnecessarily...
Regards
Eirnava...
char * s[80]; <----what's this?
CString str = "ABC";
strcpy(s, str);
->CString can covert to "const char *" but then how to convert it to "char *"? I found a way that uses strcpy. Like this:
==========================
You can simply convert it using C conversion ,(LPSTR)...or const_cast...
mahno , you'd not answer this repeatedly when the right answer was rejected unnecessarily...
Regards
Eirnava...
ASKER
sorry, I typed error. I means:
char s[80];
CString str = "ABC";
strcpy(s, str);
wyn, you suggested the use of (LPSTR). I tyied like this:
CSting s = "abc";
LPSTR t = (LPSTR)s;
but the compiler told : error C2440: 'type cast' : cannot convert from 'class CString' to 'char *'
I think CSting can not be converted directly to LPSTR. right?
char s[80];
CString str = "ABC";
strcpy(s, str);
wyn, you suggested the use of (LPSTR). I tyied like this:
CSting s = "abc";
LPSTR t = (LPSTR)s;
but the compiler told : error C2440: 'type cast' : cannot convert from 'class CString' to 'char *'
I think CSting can not be converted directly to LPSTR. right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for nietod's great help and patient. Now I learn a lot about strings and my program can work. So I think I should give my points to him.
Thanks for all of you
Thanks for all of you
You are welcome...
->In general that is not safe to do. The string information may be shared by multiple string objects. If you remove "const" you mallow this string to be changed. This may unintentionally corrupt the values stored by other string objects.
========================== ==
Thanks...
->In general that is not safe to do. The string information may be shared by multiple string objects. If you remove "const" you mallow this string to be changed. This may unintentionally corrupt the values stored by other string objects.
==========================
Thanks...
continues