分配wchar_t类型 [英] Assigning wchar_t types

查看:57
本文介绍了分配wchar_t类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将wchar_t类型的变量分配给临时变量?

在我的程序中,动态获取wchar_t类型变量的值,并且我想将它们分配给相同类型的临时变量以进行格式化.
但出现错误,无法将wchar_t转换为wchar_t.
例如:

How to assign a variable of type wchar_t to a temporary variable ?

In my program values for variables of type wchar_t are obtained dynamically and I want to assign them to a tempory variable of the same type for formatting purpose.
But I get an error, that I cannot convert wchar_t to wchar_t.
For example:

wchar_t    str1[2046];
swscanf_s(buffer, _T("%[^,]"), str1, 2046);

wchar_t strId;
strId = str1; // this is not allowed 




如何分配它们?




How can I assign them?

推荐答案

wchar_t是单个Unicode字符(16位).例如,您的strId.
wchar_t数组(如您的str1)可以容纳一个字符串.
考虑使用std::wstring来保存字符串.
其他(更糟糕)的选择是:使用指针(wchar_t *pStr = str1)从来都不是一个好主意,在您的特定情况下,这是一个非常糟糕的主意;或者,如果您完全绝对相信您的str1变量的长度是一个且只有一个字符,则可以执行以下操作:wchar_t strId = str1[0].

希望这会有所帮助,

巴勃罗.
wchar_t is a single Unicode character (16 bits). For instance, your strId.
An array of wchar_t (like your str1) can hold a string.
Consider using std::wstring to hold strings.
Other (worse) options are: using a pointer (wchar_t *pStr = str1) which is never a good idea, and in your particular case is a really bad idea; or, if you''re totally and absolutely convinced that the length of your str1 variable is one and only one character, you can do: wchar_t strId = str1[0].

Hope this helps,

Pablo.


使用指针
wchar_t * pStr = str1;

您可以尝试
using a pointer
wchar_t *pStr = str1;

you can try


明显的问题是str1是wchat_t的数组,而strId是单个wchar_t.因此,不可能将str1分配给strId,并且编译器有充分的理由抱怨.

现在,第一步是使strId和数组具有与str1相同的大小,因此请写:

The obvious problem is that str1 is an array of wchat_t and strId is a single wchar_t. So it''s impossible to assign str1 to strId and the compiler is complaining for a good reason.

Now, the first step is to make strId and array of the same size as str1, so write:

wchar_t strId[2046];



编译器仍不会将str1复制到strId中,因为数组复制不是基本的语言构造.另外,您不想复制str1的所有2046个元素,而只复制那些不超过NULL(包括NULL)字符的元素.但是一个库函数可以提供帮助,称为_tcscpy_s,它可以做到这一点:



The compiler will still not copy str1 into strId, because array copy is not a basic language construct. And in addition you don''t want to copy all 2046 elements of str1, but only those up to and including the NULL character. But a library function comes to help, which is called _tcscpy_s, which does just that:

_tcscpy_s (strId, 2046, str1);



跟踪此类数组的长度,分配它们并进行复制是一种不愉快的任务. STL库类型std :: wstring减轻了所有负担,使事情变得如此简单. wstring仍然存在的问题是由于使用此类的经验不足而导致的.我建议您看一下文档和一些示例,一切都会顺利进行.如果您仍然不走运,请粘贴您正在使用的确切代码,以便我们进行查看.



Keeping track of the length of such arrays, allocating them and copying them is kind of unpleasant task. The STL library type std::wstring takes away all that burden and makes things so much easier. The problems you are still having with wstring result from not enough experience with using this class. I''d recommend you take a look at the documentation and some examples and everything will work out fine. If you still are not in luck, paste the exact code you are using so we can have a look at it.


这篇关于分配wchar_t类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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