如何将字符串转换为char [] [英] How to convert a string to char[]

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

问题描述


可能重复:

将std :: string转换为const char *或char *




b $ b

我有一个从这个方法的txt数据的行

i've got a line from a txt data with this methode

 for ( int anrede=0;!user1.eof();anrede++){
     if (anrede==1){
         getline(user1, inhalt_anrede);       
     }
 }

现在 inhalt_anrede 应该转换为 char [5] anrede 我在程序的其他部分使用了 anrede

now inhalt_anrede should be converted to char[5] anrede i have used anrede in other parts of the program.

所以txt数据的工作方式类似于保存数据。

so the txt data works like a save-data.

我还在学习, ; D。

i'm still learning so please be tender ;D.

btw .:不,这不是我的家庭作业。我来自德国和德国没有C ++ @学校。甚至没有java或delphi。

btw.: no, this is no homework like someone says about a other question from me. i from germany and in germany there is no C++ @ schools. not even java or delphi. if ur lucky u learn HTML, thats it.

推荐答案

std::string str("abc");
char cstr[5];
strcpy(cstr, str.c_str());

确保不会溢出c字符串(在此示例中,只能包含4个chars + NULL)。

Be sure not to overflow the c string (can contain only 4 chars + NULL in this example).

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

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