Solved

convert CSting to LPTSTR

Posted on 2000-05-13
12
1,711 Views
Last Modified: 2012-06-21
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
Comment
Question by:Zhu051200
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2806790
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
 
LVL 22

Expert Comment

by:nietod
ID: 2806800
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
 

Author Comment

by:Zhu051200
ID: 2806830
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
 
LVL 1

Expert Comment

by:mahno
ID: 2807029
Hi,

Try to

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

Hope this helps,
  mahno


0
 
LVL 22

Expert Comment

by:nietod
ID: 2807035
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
 
LVL 22

Expert Comment

by:nietod
ID: 2807115
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Zhu051200
ID: 2807658
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2807749
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
 

Author Comment

by:Zhu051200
ID: 2807788
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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2808077
>> 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
 

Author Comment

by:Zhu051200
ID: 2808325
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2808415
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now