Question #25287

char psCommand[100];
sprintf(psCommand, "%s%d%s%d%s%d%s%d", "/bin/ps -f --pppid ", getppid(), ",", getpid());
system(psCommand);


These three lines of codes are written in C sharp, how would they look like in C++??

Expert's answer

/* First of all the code you gave is written in C.
Here's how will it look in C++ */

#include <string>
#include <sstream>
#include <cstdlib>

#include <unistd.h>

using namespace std;

int main()
{
ostringstream psCommand;
psCommand << "/bin/ps -f --pppid ";
psCommand << getppid() << "," << getpid();

system(psCommand.str().c_str());
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS