问题:如何在 C++ MFC 中将 CString 转换为 const char *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5493017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem: How to convert CString into const char * in C++ MFC
提问by Chicko Bueno
How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.
如何将 CString 转换为 const char *?我已经尝试了在互联网上找到的所有内容,但我仍然无法转换它们。
Please help.
请帮忙。
Thank you.
谢谢你。
回答by Gregor Brandt
CString casts to const char * directly
CString 直接转换为 const char *
CString temp;
temp = "Wow";
const char * foo = (LPCSTR) temp;
printf("%s", foo);
will print 'foo'
将打印 'foo'
Newer version of MFC also support the GetString() method:
较新版本的 MFC 也支持 GetString() 方法:
CString temp;
temp = "Wow";
const char * foo = temp.GetString();
printf("%s", foo);
回答by ildjarn
Short answer: Use the CT2CA
macro (see ATL and MFC String Conversion Macros). This will work regardless of your project's 'Character Set' setting.
简短回答:使用CT2CA
宏(请参阅ATL 和 MFC 字符串转换宏)。无论您的项目的“字符集”设置如何,这都将起作用。
Long answer:
长答案:
- If you have the
UNICODE
preprocessor symbol defined (i.e., ifTCHAR
iswchar_t
), use theCT2CA
orCW2CA
macro. - If you don't (i.e., if
TCHAR
ischar
), CString already has an operator to convert tochar const*
implicitly (seeCSimpleStringT::operator PCXSTR
).
- 如果您
UNICODE
定义了预处理器符号(即 ifTCHAR
iswchar_t
),请使用CT2CA
orCW2CA
宏。 - 如果您不这样做(即,如果
TCHAR
是char
),则 CString 已经有一个可以char const*
隐式转换为的运算符(请参阅 参考资料CSimpleStringT::operator PCXSTR
)。
回答by Jonathan Wood
If your application is not Unicode, you can simple typecast to const char *
. Use the GetBuffer()
method if you need a char *
that you can modify.
如果您的应用程序不是 Unicode,您可以简单地将类型转换为const char *
. GetBuffer()
如果您需要char *
可以修改的方法,请使用该方法。
If your application is Unicode and you really want a char *
, then you'll need to convert it. (You can use functions like MultiByteToWideChar()
.)
如果您的应用程序是 Unicode 并且您确实想要一个char *
,那么您需要转换它。(您可以使用像MultiByteToWideChar()
.)
回答by user7748179
First : define char *inputString; in your_file.h
第一:定义 char *inputString; 在 your_file.h
Second : define in yourFile.cpp : CString MyString;
第二:在 yourFile.cpp 中定义: CString MyString;
MyString = "Place here whatever you want";
inputString = new char[MyString.GetLength()];
inputString = MyString.GetBuffer(MyString.GetLength());
The last two sentences convert a CString variable to a char*; but be carefull, with CString you can hold millons of charcteres but with char* no. You have to define the size of your char* varible.
最后两句将CString变量转换为char*;但要小心,使用 CString 你可以保存数以百万计的字符,但使用 char* 没有。您必须定义 char* 变量的大小。
回答by joker
I know it's late, but I couldn't use the marked-as-answer solution. I search all over the internet and nothing worked for me. I muscled through it to get a solution:
我知道已经晚了,但我无法使用标记为答案的解决方案。我在互联网上搜索,没有任何对我有用。我强行通过它来获得解决方案:
char * convertToCharArr(CString str) {
int x = 0;
string s = "";
while (x < str.GetLength()) {
char c = str.GetAt(x++);
s += c;
}
char * output = (char *)calloc(str.GetLength() + 1, sizeof(char));
memcpy(output, s.c_str(), str.GetLength() + 1);
return output;
}