cstring to char * conversion
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
for this purpose you can use
strcpy(char *,CString)
It will help you.The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
namy1 wrote:
basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that
Well if
_UNICODE
is defined then you will have to do some additional chores for converting tochar*
(Read this[^] for more info), else it's quite easyCString csStr = "Non unicode string";
char *szStr = csStr.GetBuffer(0);AFunctionToCall( szStr );
//Now release the buffer
csStr.ReleaseBuffer();Well if you need a
const char*
, it's much more easierCString csStr = "Non unicode string";
const char* szStr = csStr; // Calls operator LPCTSTR()
AFunctionToCall( szStr );
Nibu thomas A Developer Programming tips[^] My site[^]
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
use
CString::GetBuffer()
withCString::GetLength()
to copy the enitre string into the char*.
--[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
-
You can use the operator LPCTSTR ( ), like this: CString cs = "Hello"; char * cp = (LPCTSTR) cs; Operator LPCTSTR is built in the CString class, so it's safe to use.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
hello "kakan":confused: I used the method u specified but its giving the following error C:\Program Files\Microsoft Visual Studio\MyProjects\interface\interfacedialog1.cpp(245) : error C2440: 'initializing' : cannot convert from 'const char *' to 'char *' Thanx:)
-
Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.
CString is replacable with char, ie you can put in a CString when a char[] is required. try putting in a reference to your CString ie &string
-
hello "kakan":confused: I used the method u specified but its giving the following error C:\Program Files\Microsoft Visual Studio\MyProjects\interface\interfacedialog1.cpp(245) : error C2440: 'initializing' : cannot convert from 'const char *' to 'char *' Thanx:)
Hello. That's right, operator LPCTSTR returns a const char * (The 'C' stands for 'const') The reason for returning a const char * is that the contents of what the const char * points to must be unaltered. So if you call a function that acually changes the content of the CString, then you have to use the CString methods GetBuffer() and ReleaseBuffer(). GetBuffer() returns a LPTSTR (a char *). But if you use GetBuffer(), then you must call ReleaseBuffer() So in your case, use GetBuffer() and ReleaseBuffer(). And check out the CString documentation at MSDN. I.e here[^]
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
use
CString::GetBuffer()
withCString::GetLength()
to copy the enitre string into the char*.
--[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
Unless a copy is actually what is desired, there is no need to copy the data for this. GetBuffer returns a valid char* that can be used until you call ReleaseBuffer (at which time the CString object will be updated with the changes you made to the buffer).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac