在cpp中将字符串转换为_T [英] convert string to _T in cpp

查看:195
本文介绍了在cpp中将字符串转换为_T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 string char * 转换为 _T 但不能。

I want to convert string or char* to the _T but not able to do.

如果我写

_tcscpy(cmdline,_T ("hello world")); 

它工作得很好,但如果我写

it works perfectly, but if i write

char* msg="hello world";
_tcscpy(cmdline,_T (msg));

它显示错误如:错误C2065:'Lmsg':undeclared identifier

it shows an error like: error C2065: 'Lmsg' : undeclared identifier

请给我一个解决方案。

Thanx提前。

推荐答案

_T 是一个宏,定义为(if UNICODE ):

_T is a macro, defined as (if UNICODE is defined):

#define _T(a)  L ## a

它只能与字符串文字一起使用。因此,当你写 _T(hi)它会变成 Lhi但是当你写 _T(msg)它会变成 Lmsg 这是一个未定义的标识符,你不打算

which can work only with string-literals. So when you write _T("hi") it becomes L"hi" which is valid, as expected. But when you write _T(msg) it becomes Lmsg which is an undefined identifier, and you didn't intend that.

所有您需要的是此函数 mbstowcs as:

All you need is this function mbstowcs as:

const char* msg="hello world"; //use const char*, instead of char*
wchar_t *wmsg = new wchar_t[strlen(msg)+1]; //memory allocation
mbstowcs(wmsg, msg, strlen(msg)+1);

//then use wmsg instead of msg
_tcscpy(cmdline, wmsg);

//memory deallocation - must do to avoid memory leak!
 delete []wmsg;

这篇关于在cpp中将字符串转换为_T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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