Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

convert CSting to LPTSTR

Posted on 2000-05-13
12
1,724 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

808 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