C++ 如何为 TCHAR 数组赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1547790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:22:57  来源:igfitidea点击:

How to assign a value to a TCHAR array

c++tchar

提问by Etan

I have a TCHARarray in my C++ code which I want to assign static strings to it.

TCHAR我的 C++ 代码中有一个数组,我想为其分配静态字符串。

I set an initial string to it via

我通过它设置了一个初始字符串

TCHAR myVariable[260] = TEXT("initial value");

Everything works fine on this. However, when I split it in two lines as in

一切正常。然而,当我把它分成两行时

TCHAR myVariable[260];
myVariable = TEXT("initial value");

it bugs and gives a compiler error:

它会出错并给出编译器错误:

error C2440: '=': cannot convert from 'const char [14]' to 'TCHAR [260]'

错误 C2440:“=”:无法从“const char [14]”转换为“TCHAR [260]”

shouldn't the TEXT()function do exactly what I want here? convert the given string to TCHARs? Why does it work, when putting the two lines together? What do I have to change in order to get it working?

TEXT()功能不应该完全按照我的要求执行吗?将给定的字符串转换为TCHARs? 将两条线放在一起时为什么会起作用?为了让它工作,我必须改变什么?

Some other confusing thing I have encountered:

我遇到的其他一些令人困惑的事情:

I've searched the internet for it and have seen that there are also _T()and _TEXT()and __T()and __TEXT(). What are they for? Which of them should I use in what environment?

我在互联网上搜索过它,发现还有_T()and_TEXT()__T()and __TEXT()。它们是为了什么?我应该在什么环境中使用它们中的哪一个?

回答by avakar

The reason the assignment doesn't work has very little to do with TCHARs and _T. The following won't work either.

分配不起作用的原因与TCHARs 和几乎没有关系_T。以下也行不通。

char var[260];
var = "str";   // fails

The reason is that in C and C++ you can't assign arrays directly. Instead, you have to copy the elements one by one (using, for example, strcpy, or in your case _tcscpy).

原因是在 C 和 C++ 中你不能直接分配数组。相反,您必须一个一个地复制元素(例如,使用strcpy,或者在您的情况下使用_tcscpy)。

strcpy(var, "str");

Regarding the second part of your question, TEXT, _Tand the others are macros, that in Unicode builds turn a string literal to a wide-string literal. In non-Unicode builds they do nothing.

关于你的问题,第二部分TEXT_T和其他人是宏,在Unicode版本变成一个字串宽字符串文字。在非 Unicode 构建中,它们什么都不做。

回答by D.Shawley

See avakar's answerfor the direct answer. I was going to add this as a comment, but it really is a freestanding recommendation. I will warn you up from that this will sound like a rant but it does come from using TCHARand then working issues for a few years before trying to remove it from a rather large codebase.

有关直接答案,请参阅avakar 的答案。我打算将其添加为评论,但这确实是一个独立的建议。我会警告你,这听起来像是咆哮,但它确实来自使用TCHAR和工作几年的问题,然后再尝试从相当大的代码库中删除它。

Make sure that you really understand the effect of using TCHARarrays and their friends. They are deceptively difficult to use correctly. Here is a short list of things that you have to watch out for:

确保你真正了解使用TCHAR数组及其朋友的效果。它们看似难以正确使用。以下是您必须注意的事项的简短列表:

  • sizeof(TCHAR)is conditional: Scrutinize code that contains this. If it is doing anything other than calculating the size that is being passed to malloc()or memcpy()then it is probably wrong.
  • TCHARis a type alias: Since TCHARis nothing more than a typedef, it is really easy to write things like wcscpy(tszStr, wszAry)and be none the wiser. Basically, TCHARis eithercharor wchar_tso overload selections might surprise you.
  • wsprintf()and swprintf()are different: This is a special case of the previous but it bears special consideration since it is so easy to make a mistake here!
  • sizeof(TCHAR)是有条件的:仔细检查包含此内容的代码。如果它除了计算传递给的大小之外还做任何事情,malloc()否则memcpy()它可能是错误的。
  • TCHAR是一个类型别名:因为TCHAR它只不过是一个typedef,所以写这样的东西真的很容易,wcscpy(tszStr, wszAry)而且也不聪明。基本上TCHAR要么char还是wchar_t如此超负荷的选择可能会让你大吃一惊。
  • wsprintf()并且swprintf()不同:这是前一个的特例,但需要特别考虑,因为这里很容易出错!

If you want to use TCHAR, then make sure that you compile bothUNICODE and MBCS versions regularly. If you do not do this, then you are probably going to undo any advantage that you are trying to gain by using them in the first place.

如果你想使用TCHAR,然后确保您编译双方定期UNICODE和MBCS版本。如果您不这样做,那么您很可能会取消您试图通过使用它们而获得的任何优势。

Here are two recommendations that I have for how to not use TCHARin the first place when you are writing C++ code.

以下是我TCHAR在编写 C++ 代码时首先不使用的两个建议。

  1. Use CStringif you are using MFC or are comfortable tying yourself to MSFT.
  2. Use std::stringor std::wstringin conjunction with the type-specific API - use CreateFileA()and CreateFileW()where appropriate.
  1. 使用CString如果您正在使用MFC或舒适搭售自己MSFT。
  2. 使用std::string或者std::wstring与特定类型的API相结合-使用CreateFileA()CreateFileW()在适当情况下。

Personally, I choose the latter but the character encoding and string transcoding issues are just a nightmare from time to time.

我个人选择后者,但字符编码和字符串转码问题只是时不时的噩梦。