Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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");
Posted

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 a const char* (if compiling ANSI) of const 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 the CString::GetBuffer()) method at all for such a thing !!!

So, to come back to you problem, what is your problem ?

 
Share this answer
 
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.


 
Share this answer
 
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

 
Share this answer
 
There a various ways:
1)
CString str = "filename";<br /><br />char charPtr[100];<br />sprintf(charPtr, "%s", str);

2)
CString str = "filename";<br />char* charPtr = str.GetBuffer(str.GetLength());
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900