Convert CString to const char * in embedded VC++
-
Hi all.. I've been stuck for a long time in this problem. It seems very easy to convert CString to a const char * or char * in VC++, but it really does not work for embedded VC++. My code is simply trying to open a file, using
fopen
with file path taken from CFileDialog GetPathName().CFileDialog dlg (TRUE, _T("maf"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER, szFilters); if (IDOK == dlg.DoModal()) { CString __filepath = dlg.GetPathName(); file = fopen(__filepath, "rb"); }
I've tried to read each single character from CString (by iteratively usingGetAt()
function) as followsCString __filepath; __filepath = dlg.GetPathName(); int x = __filepath.GetLength(); char *filename; filename = new char [x]; for (int i=0;i<x;i++) { filename[i] = (char)__filepath.GetAt(i); }
But it does not work for UNICODE text. Help me.. :(( Houarihouari_id wrote:
file = fopen(__filepath, "rb");
What is the value of
__filepath
at this point? Whenfopen()
fails, what is the value oferrno
?houari_id wrote:
file = fopen(__filepath, "rb");
. If you are using Unicode, use
_wfopen()
instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
why u want to use char*. If u use TCHAR*..the problem will be solved..
houari_id wrote:
CString __filepath; __filepath = dlg.GetPathName(); int x = __filepath.GetLength(); char *filename; filename = new char [x]; for (int i=0;i This is not a good way.. Use
_tcscpy
instead nave -
I've tried this way:
char *filename; filename = new char [x]; _tcscpy(filename, __filepath);
And when I compile the code, this error occurs:error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'unsigned short *'
Houari -
I've tried this way:
char *filename; filename = new char [x]; _tcscpy(filename, __filepath);
And when I compile the code, this error occurs:error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'unsigned short *'
Houarihouari_id wrote:
char *filename; filename = new char [x];
With Unicode, use
wchar_t
instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
houari_id wrote:
file = fopen(__filepath, "rb");
What is the value of
__filepath
at this point? Whenfopen()
fails, what is the value oferrno
?houari_id wrote:
file = fopen(__filepath, "rb");
. If you are using Unicode, use
_wfopen()
instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
This error occurs during the compiling the code:
error C2664: 'fopen' : cannot convert parameter 1 from 'class CString' to 'const char *'
Actually, it is fine in VC++ isn't it? In MFC, I just putfile = fopen (dlg.GetPathName(), "rb");
and it's work fine. But in eVC++, this seems not OK. Houari -
Thank you for your suggest, Viorel. But when I compile the code, this error still occurs:
error C2664: 'fopen' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *'
I also tried using TCHAR as Milton suggest, the error becomeserror C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *'
HouariFrom the first error messages it seems that your application is actually non-Unicode. See the project properties in order to check this (General --> Character Set). The solution with
TCHAR
should work in both cases, so the second error message is strange. -- modified at 9:24 Tuesday 20th June, 2006 Actually in order to work in both cases, you should use_tfopen
instead offopen
and_wfopen
. Instead ofchar
orwchar_t
, useTCHAR
. Define literal strings with_T("...")
macro. -
houari_id wrote:
char *filename; filename = new char [x];
With Unicode, use
wchar_t
instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
This error occurs during the compiling the code:
error C2664: 'fopen' : cannot convert parameter 1 from 'class CString' to 'const char *'
Actually, it is fine in VC++ isn't it? In MFC, I just putfile = fopen (dlg.GetPathName(), "rb");
and it's work fine. But in eVC++, this seems not OK. Houaritry, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB
-
try, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB
-
Actually in order to work in both cases, you should use
_tfopen
instead offopen
and_wfopen
:file = _tfopen(dlg.GetPathName(), _T("rb"));
-
No, both data type TCHAR and wchar_t are not working, this error still there for both:
cannot convert parameter 1 from 'unsigned short *' to 'const char *'
Or is it something wrong with my embedded VC++ ? :(( HouariAre you still trying to use
fopen()
? If so, you are erroneously mixing MBCS and Unicode.// MBCS
char szFile[MAX_PATH];
fopen(szFile, "r");// Unicode
wchar_t szFile[MAX_PATH];
_wfopen(szFile, "r");// Portable
TCHAR szFile[MAX_PATH];
_tfopen(szFile, "r");
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
try, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB
Milton KB wrote:
file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb");
There is no need to call
GetBuffer()
as the object is not being modified.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi all.. I've been stuck for a long time in this problem. It seems very easy to convert CString to a const char * or char * in VC++, but it really does not work for embedded VC++. My code is simply trying to open a file, using
fopen
with file path taken from CFileDialog GetPathName().CFileDialog dlg (TRUE, _T("maf"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER, szFilters); if (IDOK == dlg.DoModal()) { CString __filepath = dlg.GetPathName(); file = fopen(__filepath, "rb"); }
I've tried to read each single character from CString (by iteratively usingGetAt()
function) as followsCString __filepath; __filepath = dlg.GetPathName(); int x = __filepath.GetLength(); char *filename; filename = new char [x]; for (int i=0;i<x;i++) { filename[i] = (char)__filepath.GetAt(i); }
But it does not work for UNICODE text. Help me.. :(( HouariYou need to read up on character sets, and the differences between ANSI and Unicode builds. Start here: clickety[^]
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer