CString to const char* conversion
-
I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.
-
I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.
I'm not sure if this will work, but have you tried simply setting the LPTSTR equal to the CString? I think CString has an LPTSTR operator if memory serves... -Kmaz
-
I'm not sure if this will work, but have you tried simply setting the LPTSTR equal to the CString? I think CString has an LPTSTR operator if memory serves... -Kmaz
CString is not able to be assigned with LPTSTR, LPTSTR lpszString = filename; gives error error C2440: 'initializing' : cannot convert from 'class CString' to 'unsigned short *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
-
I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.
CString has an LPCTSTR operator, so you can pass a CString object to any function that requires a LPCTSTR
fptr = fopen(m_fileName, _T("r"))
CString::GetBuffer()
returns a LPTSTR, not a LPCTSTR. LPTSTR is achar*
LPCTSTR is aconst char*
[
](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
-
CString has an LPCTSTR operator, so you can pass a CString object to any function that requires a LPCTSTR
fptr = fopen(m_fileName, _T("r"))
CString::GetBuffer()
returns a LPTSTR, not a LPCTSTR. LPTSTR is achar*
LPCTSTR is aconst char*
[
](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
m_fileName is CEdit type. Also i hv to collect string value from CstringArray like this.. CStringArray* aXMLfiles; CString filename; int mlen = aXMLfiles->GetSize(); // write all the XML files for(j=0;jGetAt(j); LPTCSTR lpszString = filename; fptr = fopen( lpszString, "r" ); ////do something//// } still i get error.
-
m_fileName is CEdit type. Also i hv to collect string value from CstringArray like this.. CStringArray* aXMLfiles; CString filename; int mlen = aXMLfiles->GetSize(); // write all the XML files for(j=0;jGetAt(j); LPTCSTR lpszString = filename; fptr = fopen( lpszString, "r" ); ////do something//// } still i get error.
You can take away the extra assignment, this is enough:
fopen( filename, "r" );
or this:
fopen( ( LPCTSTR ) filename, "r" );
If not, your problem lies elswhere, and you might want to post some more code.
-
You can take away the extra assignment, this is enough:
fopen( filename, "r" );
or this:
fopen( ( LPCTSTR ) filename, "r" );
If not, your problem lies elswhere, and you might want to post some more code.
still problem continues.I put the code for ur ref. CStringArray* aXMLfiles; int mlen,j; CString filename; FILE *fptr; aXMLfiles = GetFileNames("*.XML"); /// This function retrives me all XML files.. mlen = aXMLfiles->GetSize(); for(j=0;jGetAt(j); fptr = fopen( ( LPCTSTR ) filename, "r" ); if (fptr != NULL){ // do something } } fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
-
still problem continues.I put the code for ur ref. CStringArray* aXMLfiles; int mlen,j; CString filename; FILE *fptr; aXMLfiles = GetFileNames("*.XML"); /// This function retrives me all XML files.. mlen = aXMLfiles->GetSize(); for(j=0;jGetAt(j); fptr = fopen( ( LPCTSTR ) filename, "r" ); if (fptr != NULL){ // do something } } fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
As
CString
has a user-defined operatorLPCTSTR
, the "or the operator cannot be called"-part of the error message seems to be the interesting part. What is the error number (CXXXX) you get? -
I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.
-
I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.
This is why i stopped using CString. It's an all or nothing proposition. If you compile for UNICODE then it wraps an array of wchar_t (unsigned short). If you compile for ANSI then it wraps an array of char. You can't cast between the two, you must convert. Use wcstombs to go from a wchar_t to char. Use mbstowcs to go from a char to wchar_t. [EDIT] Actually what you should do if using CString is go the whole way and use all the _t funtion versions. e.g. _tfopen() will work with a CString regardless of it being UNICODE or ANSI. Most functions that take a char* have a _t*() version. Check the MSDN doc's. [/EDIT] ...cmk Save the whales - collect the whole set