简体   繁体   中英

Cannot convert std::stringstream<char> to const char*

I tried to run command with help of system() function, forwarding argument as in code below:

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*.

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. So what to do?

volume_control.str() returns a std::string type. You need to call std::string::c_str() orstd::string::data() method on it.

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(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM