|
i need to extract the data to form a Tree ,i must transfer the cstring into char*
i use (lpstr) but compile note not well
so i myself write a function as following:
char* CSelectDlg::change(CString mystr)
{ int len=mystr.GetLength();
char* array=new char(len);
for(int i=0;i
|
|
|
|
|
CString str;
const char *cp = (LPCSTR) str;
char *p = (LPSTR) ((LPCSTR) str);
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
|
I was not kidding at all. The link you provided is not http. Want to try again?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
|
CString str;<br />
const char *cp = (LPCSTR) str;<br />
char *p = (LPSTR) ((LPCSTR) str);
Truth be told, he answered the Original Poster's question perfectly. The OP did not ask if it was safe or even a good idea to cast a CString into a non-const char* ...! (Which, of course, we all know is generally not a good idea at all!)
Peace!
-=- James
Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
hi:your advice is ok by tring ,but can you explain why my method has some bug?
in addition i seldom have conscious to use LPCSTR LPSTR LPSTR , Please you give me a simple introduction about when to use them ?
thx again
|
|
|
|
|
There are only two ways of getting at the internal CString buffer. One is to use the LPCSTR operator and the other is to use the GetBuffer() method. One is read-only while the other is writeable. Since you did not indicate a need to update the buffer, I provided you with the LPCSTR way.
vividtang wrote:
i seldom have conscious to use LPCSTR LPSTR LPSTR
I do not understand this. Do you mean you seldom have a need to use them?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
vividtang wrote:
in addition i seldom have conscious to use LPCSTR LPSTR LPSTR , Please you give me a simple introduction about when to use them ?
Use them all the time ! MFC does
|
|
|
|
|
int len=mystr.GetLength() + 1;
char* array=new char(len);
lstrcpy(array, mystr);
or
CString s( "abcd" );
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );
s.ReleaseBuffer( );
|
|
|
|
|
CString s( "abcd" );<br />
LPTSTR p = s.GetBuffer( 10 );<br />
strcpy( p, "Hello" );
ReleaseBuffer( );
Yes, yet another bad example from MSDN! No enforcement of the buffer size obtained (should have used strncpy(...) ), and no exception handling for example.
IOW, typical high-quality MSDN documentation/examples that newer developers can learn all kinds of bad habits from!
Peace!
-=- James
Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
James R. Twine wrote:
Yes, yet another bad example from MSDN!
A problem that they have acknoledged in MSDN Magazine Editors Note i believe. The site is huge with years of information built up. They are addressing the issue. Until then you can find newer resources there if you try.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/strsafe.asp[^]
James R. Twine wrote:
typical high-quality MSDN documentation/examples that newer developers can learn all kinds of bad habits from!
Please point us to the more complete more correct online resource that we should be using instead of MSDN
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
palbano wrote:
Please point us to the more complete more correct online resource that we should be using instead of MSDN
I thought that's what CP is for
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
The strsafe functions are somewhat new, coming out right after the "secure code initiative" (or whatever it was) stuff that MS was touting a few years back after the release of .Net. That is a loooong time from when much of the bad examples out there were written, so if "they are addressing the issue", it sure took a long time... Besides, that is but one of many kinds of bad examples.
palbano wrote:
Please point us to the more complete more correct online resource that we should be using instead of MSDN
No problem: Common Sense.[^] What is more "online" than your own brain? (And did you really need a pointer to that? )
-=- James
Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
i try the first method ,but to my disappointed it did not run fine and pop a waring box to terminate my program
|
|
|
|
|
Try this instead:
int len=mystr.GetLength() + 1;
char* array=new char[len];
lstrcpy(array, mystr);
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Use GetBuffer() and when you're done call ReleaseBuffer(). That's the proper way to do it.
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
Unless there is no need to change the buffer, then the LPCSTR operator is the proper way.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
DavidCrow wrote:
Unless there is no need to change the buffer, then the LPCSTR operator is the proper way.
If there is no need to change the buffer, then it should be explicitly const , in which case you can perform a type-safe cast such as:
CString szString("abcd");<br />
LPCTSTR lpszStr = static_cast<LPCTSTR>(szString);
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
So what is the difference between:
LPCTSTR lpszStr = static_cast<LPCTSTR>(szString); and
LPCTSTR lpszStr = (LPCTSTR) szString;
They both appear to generate the same code. For the purpose of getting read-only access to the internal CString buffer, I'm not seeing a benefit in using static_cast over a traditional cast.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
you mean the advice above ,but i try that method all are dirty shown my UI dialog ,can you speak clearly? thx
|
|
|
|
|
vividtang wrote:
can you speak clearly?
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
Let the programmer do the thinking, let the compiler do the work
You can pass a CString object to any function that expects a LPCTSTR, the compiler will extract a pointer to the CString's internal buffer so all you need to do is ...
void foo( LPCTSTR lpszBuffer )<br />
{<br />
}<br />
<br />
<br />
CString strData(_T("12345"));<br />
foo(strData);
The LPCTSTR data type is a pointer to a constant string (ansi or unicode), if you dont have UNICODE defined, LPCTSTR maps to LPCSTR which is defined as "a Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters" i.e const char *
OR, if all you want to do is to copy the CString buffer to a char[] use strcpy(), strncpy, or _tcscpy(), _tcsncpy() e.g. strcpy(MyBuffer, LPCTSTR(MyCString));
|
|
|
|
|
I have never used them, but how do you arrange for them to be displayed.
My MFC appication has one defined and I have placed some info in it, but then thought how do you display and close it?
|
|
|
|
|
huh ? what Box ? if your MFC app. defined one, maybe you can tell us more about the Box in question ?
oh, it's the "About Box" silly me ...
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|