3

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?

3 Answers3

4

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

A google search would have easily given you the answer.

system(volume_control.str().c_str());
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

You should use c_str method on string returned from str method.

system(volume_control.str().c_str());
ForEveR
  • 55,233
  • 2
  • 119
  • 133
0

You can refer to this link stringstream, string, and char* conversion confusion

{ const std::string& tmp = stringstream.str(); const char* cstr = tmp.c_str(); }