[英]C++ How can I convert wchar_t* to TCHAR [] (not TCHAR*)
I wants to add a small additional options to a big unit, so I do not want to process a large amount of code. 我想在一个大单元中添加一些小的附加选项,所以我不想处理大量代码。
TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
GetTempPath(MAX_PATH, // length of the buffer
lpTempPathBuffer); // buffer for path
GetTempFileName(lpTempPathBuffer, // directory for tmp files
TEXT("QRCode_"), // temp file name prefix
0, // create unique name
szTempFileName); // buffer for name
I want to change szTempFileName to optional wstring/std::string/wchar_t* parametr . 我想将szTempFileName更改为可选的wstring / std :: string / wchar_t * parametr。
Solution: 解:
change TCHAR to wchar_t 将TCHAR更改为wchar_t
wcscpy(wchar_t[], wchat_t*); wcscpy(wchar_t [],wchat_t *);
Typically, there's no reason to use TCHAR
s at all. 通常,根本没有理由使用
TCHAR
。 If you're working with wchar_t
anyway, simply call the Unicode variants of the Winapi functions directly by adding a W
suffix: 如果仍然使用
wchar_t
,只需添加一个W
后缀即可直接调用Winapi函数的Unicode变体 :
// Use wchar_t instead of TCHAR.
wchar_t szTempFileName[MAX_PATH];
wchar_t lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
// Call GetTempPathW directly.
GetTempPathW(MAX_PATH, lpTempPathBuffer);
// Use L"..." instead of TEXT("...").
GetTempFileNameW(lpTempPathBuffer, L"QRCode_", 0, szTempFileName);
Stick with what you have and convert to the appropriate string type at the end. 坚持使用现有内容,最后将其转换为适当的字符串类型。 Getting windows to play nicely and correctly with the C++ string types will probably turn your hair grey.
使Windows在C ++字符串类型中正确正确地播放可能会使您的头发变灰。
Use the following typedef to create your string type that you can use throughout your code instead of std::string or std::wstring: 使用以下typedef创建可在整个代码中使用的字符串类型,而不是std :: string或std :: wstring:
typedef std::basic_string<TCHAR> tstring;
Construct your string from the buffer directly: 直接从缓冲区构造字符串:
tstring RealFileName(szTempFileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.