[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1760
  • Last Modified:

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).GetBuffer(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?
0
Zhu051200
Asked:
Zhu051200
  • 5
  • 4
  • 2
  • +1
1 Solution
 
nietodCommented:
A "LPSTR" and a "char *" are the same thing.  they are pointers to the start of an array that contains a NUL terminated string of characters.   a "CString" or a "string" are string classes that represent or stores a string of characters.  String classes are much much easier to use, because they can expand their storage space so to accomodate nearly any size string (with the string pointer type strings you must not store a string longer than the array) and because they have the same semantics as ordinary objects (they can be passed by value to a function or can copied using operator =, for example.)

continues
0
 
nietodCommented:
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.
0
 
Zhu051200Author Commented:
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?

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mahnoCommented:
Hi,

Try to

CSting S = "ABC";
LPSTR  P = (LPSTR)(LPCTSTR)S;

Hope this helps,
  mahno


0
 
nietodCommented:
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.
0
 
nietodCommented:
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?
0
 
Zhu051200Author Commented:
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.

0
 
WynCommented:
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...
0
 
Zhu051200Author Commented:
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?

0
 
nietodCommented:
>> but then how to convert it to "char *"?
One way is the way you found with using strcpy().  This is a bit problematic if you don't have an idea of the maximum string size.  You can get arround this using dynamic allocation like

CString str = "ABC";
int Len = str.GetLength();
char *s = new char[Len + 1];

strcpy(s, str);

   *    *    *
delete [] s;


Another option is to use the GetBuffer() member procedure to get a pointer to the string's character array.  This is a non-constant pointer.  When you are done with the pointer you must call ReleaseBuffer().   Note that this procedure allows you to change the string stored in the CString.  i.e if you change the characters that the pointer points to, the value in the CString will be changed.  (This may be desirable or it might not be.)  Also note that you may not manipulate the CString object between the calls to GetBuffer() and ReleaseBuffer().

>> You can simply convert it using C
>> conversion ,(LPSTR)...or const_cast...
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.
0
 
Zhu051200Author Commented:
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
0
 
WynCommented:
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...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now