[英]Cannot convert std::stringstream<char> to const char*
I tried to run command with help of system() function, forwarding argument as in code below:我试图在 system() 函数的帮助下运行命令,转发参数如下:
std::stringstream volume_control;
int volume_value = 5;
volume_control << "/usr/bin/amixer cset numid=1 " << volume_value << std::endl;
system(volume_control.str());
It doesn't work, because of unsuccessful conversion of std::stringstream to const char*.它不起作用,因为 std::stringstream 转换为 const char* 失败。
I know that in std::string case I have method std::c_string() and If I'm right it returns exactly what I need const char* type, but in this case of stringstream that method does not exist.我知道在 std::string 的情况下我有方法 std::c_string() ,如果我是对的,它会返回我需要的 const char* 类型,但在 stringstream 的情况下,该方法不存在。 So what to do?那么该怎么办?
volume_control.str()
returns a std::string
type. volume_control.str()
返回一个std::string
类型。 You need to call std::string::c_str()
orstd::string::data()
method on it.您需要对其调用std::string::c_str()
或std::string::data()
方法。
A google search would have easily given you the answer.谷歌搜索很容易给你答案。
system(volume_control.str().c_str());
您应该对从str
方法返回的string
使用c_str方法。
system(volume_control.str().c_str());
可以参考这个链接stringstream, string, and char* 转换混淆
{ const std::string& tmp = stringstream.str(); const char* cstr = tmp.c_str(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.