Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use std::string as command with the system() call?

Tags:

c++

I am trying to pass a string into the system() shell command function. I am trying to pass a concatenated string into it like so:

string parameters = "Doug";
system("ps -ef|grep " + parameters);

It keeps giving me error because system() takes a char*. How would I go about having the system() function work. I tried putting parameters.c_str() but doesn't work. Thanks!

like image 983
Bob Richardson Avatar asked Dec 02 '25 21:12

Bob Richardson


1 Answers

You need to call c_str() on the result of the concatenation:

system(("ps -ef|grep " + parameters).c_str());

(Note the parentheses.)

like image 145
NPE Avatar answered Dec 04 '25 12:12

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!