将CString变量传递给char变量 [英] pass CString variable to char variable

查看:148
本文介绍了将CString变量传递给char变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我想知道我进入一个变量,假设在这个变量中CString testCStr我一步一步地得到某人的名字

我想在字符类型数组中传递此名称
就像

Hi All


I want to know that i get in a variable suppose CString testCStr in this variable i get the names of somes step by step

i want to pass this name in an array of character type
like

  char name[];

name=testCStr;// at this point it gives me error


我想将CString 变量命名为字符类型数组,并将其用于进一步的操作.

如何在CString(testCStr)的变量中传递strign.到char aray变量名称;

plz帮助


i want to name of CString variable into character type array and use it further operations.

how to transfer the strign in the variable of CString(testCStr). to the char aray variable name;

plz Help

推荐答案

如果确实需要(大多数情况下不需要),则必须复制CString内容.
(为简单起见,我假设您有一个ANSI CString,而不是宽字符).
If you really need that (in most cases you don''t) then you have to copy the CString content.
(For simplicity I assume you have a ANSI CString, not a wide character one)
const int SIZE = 256;
char name[SIZE];

strncpy(name, testCStr, SIZE-1);
name[SIZE] = '\0';


如果您不打算修改字符串内容,则只需将CString强制转换为const指针即可.

If you''re not going to modify the string contents, then you can simply cast the CString to a const pointer.

const char * name = (const char *) testCStr;

Or better,

LPCTSTR name = (LPCTSTR) testCStr;


您可以使用 sprintf()函数

You can use sprintf() function

CString testCStr;

testCStr = "This is an example.";

char * chr = new char[testCStr.GetLength()];

sprintf(chr, "%s", testCStr);


这篇关于将CString变量传递给char变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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