Converting CString to char*
-
Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");
-
Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");
It should do. So, what's wrong ? Do you get an error ? CString has an inner cast Operator (
LPCTSTR
) which converts the CString object to aconst char*
(if compiling ANSI) ofconst wchar_t*
(if building unicode), so you should not have to do any much stuff. And please (if you get this advice from anywhere), don't use theCString::GetBuffer()
) method at all for such a thing !!! So, to come back to you problem, what is your problem ?
-
Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");
There a various ways: 1)
CString str = "filename";
char charPtr[100];
sprintf(charPtr, "%s", str);CString str = "filename";
char* charPtr = str.GetBuffer(str.GetLength()); -
It should do. So, what's wrong ? Do you get an error ? CString has an inner cast Operator (
LPCTSTR
) which converts the CString object to aconst char*
(if compiling ANSI) ofconst wchar_t*
(if building unicode), so you should not have to do any much stuff. And please (if you get this advice from anywhere), don't use theCString::GetBuffer()
) method at all for such a thing !!! So, to come back to you problem, what is your problem ?
Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)
-
Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");
Gut Mikh Tappe wrote:
Please let me know what to do
Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.
led mike
-
Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)
GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");
In addition to super_ttd's reply... If you must use fopen(), then you should probably use a CStringA. CString has a generic internal character type, depending on whether UNICODE or _UNICODE is defined. If you want to use the generic text CString, then it will pair nicely with the generic text version of fopen(), which is _tfopen().
Gut Mikh Tappe wrote:
char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);
Bad cast!! GetModuleFileName takes a LPTSTR as its second parameter. Your buffer, therefore, should be a TCHAR type, not char. Mind your types.....if you need a cast to get something to compile, look at WHY. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
led mike wrote:
You have lunch yet?
Heh. I'm going to HAVE to now ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
led mike wrote:
You have lunch yet?
Heh. I'm going to HAVE to now ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks It was great
-
Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)
Read the documentation about CString::GetBuffer and you will see why you have to absolutely avoids it if you don't know exactly what you are doing.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
It should do. So, what's wrong ? Do you get an error ? CString has an inner cast Operator (
LPCTSTR
) which converts the CString object to aconst char*
(if compiling ANSI) ofconst wchar_t*
(if building unicode), so you should not have to do any much stuff. And please (if you get this advice from anywhere), don't use theCString::GetBuffer()
) method at all for such a thing !!! So, to come back to you problem, what is your problem ?
Visual C++ 2005 doesn't support (LPCTSTR) cast operator. thus I need an interface to change a CString object to a char* exactly now I am porting an project from VC6 to VC2005 and it is my problem Regards Mahdi
-
GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards
-
In addition to super_ttd's reply... If you must use fopen(), then you should probably use a CStringA. CString has a generic internal character type, depending on whether UNICODE or _UNICODE is defined. If you want to use the generic text CString, then it will pair nicely with the generic text version of fopen(), which is _tfopen().
Gut Mikh Tappe wrote:
char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);
Bad cast!! GetModuleFileName takes a LPTSTR as its second parameter. Your buffer, therefore, should be a TCHAR type, not char. Mind your types.....if you need a cast to get something to compile, look at WHY. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
May you please give me an article about UNICODE ? Regards Mahdi
-
Gut Mikh Tappe wrote:
Please let me know what to do
Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.
led mike
led mike wrote:
Also reading this might help as well.[^]
I think that is the best choice to start with :)
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards
Yes, it is simply because UNICODE is defined by default (which was not the case with VC6). I strongly suggest that you read the article given by Led Mike, things will be much more clear afterwards.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards
The operator is a member of the CSimpleStringT class, which is the base class of CStringT. The actual operator is PCXSTR (the ATL type), which is the equivalent of LPCTSTR (a Win32 type). Here's your code using generic text types:
TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,MAX_PATH);
CString path = buffer;
path = path.Left(path.ReverseFind(_T('\\'))+1);
path += _T("result.mp3");
f = _tfopen( path , _T("wb"));// <-- implicitly uses CString's PCXSTR operator!
See Generic-Text Mappings in Tchar.h[^] and for Unicode info: International Programming[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Gut Mikh Tappe wrote:
Please let me know what to do
Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.
led mike
led mike wrote:
Also reading this might help as well.[^]
That IS a good link. I bookmarked that to post in the future, Thanks! :beer:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
May you please give me an article about UNICODE ? Regards Mahdi
-
There a various ways: 1)
CString str = "filename";
char charPtr[100];
sprintf(charPtr, "%s", str);CString str = "filename";
char* charPtr = str.GetBuffer(str.GetLength());