将char *转换为std :: string [英] convert a char* to std::string

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

问题描述

我需要使用 std :: string 来存储由 fgets()检索的数据。为此,我需要将 fgets()char * 输出转换为 std :: string 。如何做到这一点?

I need to use std::string to store data retrieved by fgets(). To do this I need to convert fgets() char* output into an std::string to store in an array. How can this be done?

推荐答案

std :: string 构造函数为:

const char *s = "Hello, World!";
std::string str(s);

只要确保您的 char * isn 't NULL ,否则行为未定义。

Just make sure that your char * isn't NULL, or else the behavior is undefined.

这篇关于将char *转换为std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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