C++ system

From wikinotes

system(command) using a variable

The C++ string class, part of the std namespace, allows you to manipulate strings safely. These are defined by 'std::string stringname = ""'

#include <string>

int main()
{
  std::string string1 = "echo";
  std::string string2 = "It works !!";
  std::string string3 = string1+" "+string2;
  system(string3.c_str());
}


system(command) with return value: see popen

#define DATA_SIZE 512  
  
int main(){

    char data[DATA_SIZE];
    string computerName;	

    cmdprompt = popen("hostname", "r");					//popen opens a new pipe

        fgets(data, DATA_SIZE , cmdprompt);				//fgets ( (variable to store stream) , (#of chars to read - 1) , (variable storing source) )
									//fgets pulls one entire line of code, the max number of characters depending on which it reaches first.

    pclose(cmdprompt); 							//closes the pipe
    computerName = data;

}