Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL
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 10-Oct-08 8:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 ?

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.


  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

There a various ways:
1)
CString str = "filename";

char charPtr[100];
sprintf(charPtr, "%s", str);

2)
CString str = "filename";
char* charPtr = str.GetBuffer(str.GetLength());
  Permalink  


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 10 Oct 2008
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100