本文介绍了错误:无法从"const char [12]"转换为"const wchar_t *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个简单的代码中,我遇到了转换问题,并得到了上面的错误,但是不明白为什么!有什么想法..

In a simple code, I have the conversion problem, and get the above error,but don''t understand why! Any thoughts..

CString s ="first:25.5,second,15";
if ( swscanf_s(s, "%s %f %s %d", &st1, &doub, &st2, &integ )!= 4 )

推荐答案

CString s =L"first:25.5,second,15";

或可怕地使用微软在1994年左右推出的_T宏:

or use the ghastly _T macros that Microsoft introduced in about 1994:

CString s = _T("Your guff in here...");



如果您无法定义_UNICODE UNICODE ,则可以将您正在使用的函数改为sscanf -处理狭窄的数组.

哦,当您对类型进行排序时,您可能会发现它无法按照您希望的方式工作.但这是另一个问题:-)

[您的格式字符串看起来像是sscanf,而不是swscanf_s ,并且由于字段之间没有空格,您可能会将所有内容读入第一个字符缓冲区.]



If you can''t define _UNICODE or UNICODE then perhaps change the function you''re using to sscanf instead - which deals with narrow arrays.

Oh, and when you do sort out the types you''ll probably find it won''t work the way you want it to either. But that''s another problem :-)

[Your format string looks like it''s for sscanf, not swscanf_s and as there''s no white space between fields you''re probably going to read everything into the first character buffer.]


if ( swscanf_s(s, L"%s %f %s %d", &st1, &doub, &st2, &integ )!= 4 )



正如Nithin在其帖子中所写的那样,第二个参数必须是一个宽字符串,因此必须使用L....

所有其他建议或多或少都没有用.例如,行



As Nithin has written in his post, the second argument needs to be a wide character string and hence the L"... is required.

All other suggestions are more or less useless. For example, the line

CString s ="first:25.5,second,15";



没关系,因为CString知道如何从字符型字符串转换为宽字符型字符串.一切都好.

但是,在此示例中,还有其他一些怪癖.首先,如果使用的是swscanf _s ,则字符串参数需要缓冲区长度的附加说明符.例如:



it okay, because the CString knows how to convert from a char string to a wide-char string. All is ok there.

There are however a couple of other quirks in this example. First of all, if you are using swscanf_s the string arguments require an additional specifier of the buffer length. For example:

wchar_t st1 [50];
wchar_t st2 [50];
if (swscanf_s(s, L"%s %f %s %d", &st1, 50, &doub, &st2, 50, &integ )!= 4 )



接下来,您输入的内容与格式说明符不完全匹配. %s消耗所有字符,直到下一个空格.因此,您最好将输入更改为:



Next, your input does not really match the format specifier. The %s consumes all characters up to the next white space. Hence you better change the input to:

CString s ="first 25.5 second 15";



最后,如果doub是双精度变量,则应将%lf"指定为格式说明符,否则swscanf_s假定您正在传递浮点变量的地址.

经过所有这些更改,示例将按预期运行.



And finally, if doub is a double variable, you should specify "%lf" as format specifier, otherwise swscanf_s assumes that you are passing the address of a float variable.

With all these changes the example will run as expected.


CString s ="first:25.5,second,15";
if ( swscanf_s(s, "%s %f %s %d", &st1, &doub, &st2, &integ )!= 4 )



至:



to:

CString s =_T("first:25.5,second,15");
if ( swscanf_s(s, _T("%s %f %s %d"), &st1, &doub, &st2, &integ )!= 4 )







or

CString s =L"first:25.5,second,15";
if ( swscanf_s(s, L"%s %f %s %d", &st1, &doub, &st2, &integ )!= 4 )




顺便说一句,我认为这是因为Unicode转换问题. _T将帮助您的代码在需要时自动从Unicode和ANSI转换.您仍然可以使用L这是示例字符串"代替_T(这是示例字符串").




By the way I think it is because of a Unicode conversion issue. The _T will help your code automatically convert from Unicode and ANSI when they are required. You can still use L"This is a sample string" instead of _T("This is a sample string").


这篇关于错误:无法从"const char [12]"转换为"const wchar_t *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:02