Convert char* to CString
-
Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.
-
Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.
_base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)
-
_base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)
struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE; FILE *fleCredentials; fleCredentials = fopen("credentials.crd", "a+"); fprintf(fleCredentials, "%s ",(LPCTSTR)m_Username ); Problem: ======== Only one character of "m_Username" has to be stored in the "credentials.crd". I would like to store the fullname into the file. Help to proceed further.
-
_base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)
Sunil Shindekar wrote:
_base = m_userName.GetBuffer
The above is a mistake, because, unless you want to lock the
CString
internal buffer forever, then you have to callReleaseBuffer
method, afterwards the_base
pointer becomes garbage, please read documentation http://msdn2.microsoft.com/en-us/library/kt26tkzx(VS.71).aspx[^].Sunil Shindekar wrote:
_base = new char[] strcpy(_base, m_userName.getbuffer)
Better, but still mistakes. You don't need at all
GetBuffer
, to do this (and you aren't callingReleaseBuffer
afterwards), theLPCTSTR
cast operator is enough. Furthermore you don't specify character array size. And, finally, you're supposing that it is an ANSI build. Is It enough? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.
Provided it is an
ANSI
(notUNICODE
build, you can do:CString strFoo("foo");
struct _iobuf iobuf;iobuf._ptr = new[strFoo.GetLenght()+1];
if (iobuf._ptr)
{
strcpy(iobuf._ptr, strFoo);
}
...
...// don't forget to delete the buffer
if (iobuf._ptr)
{
delete [] iobuf._ptr;
}However, I suggest, if you can, to refactor a bit you project (i.e. avoiding such a mixing of string classes and character arrays. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Provided it is an
ANSI
(notUNICODE
build, you can do:CString strFoo("foo");
struct _iobuf iobuf;iobuf._ptr = new[strFoo.GetLenght()+1];
if (iobuf._ptr)
{
strcpy(iobuf._ptr, strFoo);
}
...
...// don't forget to delete the buffer
if (iobuf._ptr)
{
delete [] iobuf._ptr;
}However, I suggest, if you can, to refactor a bit you project (i.e. avoiding such a mixing of string classes and character arrays. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.