将CString转换为char * [英] Converting CString to char*

查看:652
本文介绍了将CString转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有
在Visual C ++ 2005中,我想将CString文件更改为char *
我使用了以下代码
,但fopen的第一个参数需要char *
请让我知道该怎么办
注意事项
char buffer [MAX_PATH];
GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);
CString path = buffer;
path = path .Left(path.ReverseFind(''\\'')+ 1);
路径+ ="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");

推荐答案

应该这样做.

那么,怎么了?您是否收到错误消息?

CString具有内部强制转换运算符(LPCTSTR),该运算符将CString对象转换为const wchar_t*const char*(如果编译ANSI)(如果构建unicode),那么您不应该做太多的事情.

请(如果您从任何地方得到此建议),请不要完全使用CString::GetBuffer())方法!
那么,回到您的问题上,您的问题是什么?

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 ?


Gut Mikh Tappe写道:
Gut Mikh Tappe wrote:

请让我知道该怎么做



好的,我会尝试的.您需要做的是阅读文档. [ ^ ]

也可以阅读这篇文章. [ ^ ]

根据我的经验,并非所有作者与每个读者匹配.因此,如果您发现阅读这些资源不足以满足您的需求,那么100%会继续寻找更多资源.最重要的是您需要阅读和研究而不是键入代码和论坛消息.



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.






除了super_ttd的回复......

如果必须使用fopen(),则可能应该使用CStringA.

CString具有通用的内部字符类型,

如果要使用通用文本CString,则它将与fopen()的通用文本版本很好地配对,该版本是_tfopen( ).


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写道:
Gut Mikh Tappe wrote:

char buffer [MAX_PATH];
GetModuleFileName(NULL,( LPWCH)buffer,MAX_PATH);

char buffer[MAX_PATH];
GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);



效果不好! GetModuleFileName将LPTSTR作为其第二个参数.因此,您的缓冲区应该是TCHAR类型,而不是char.

请注意类型.....如果您需要强制转换以获取要编译的内容,请查看WHY.

标记



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


这篇关于将CString转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆