title: How to pass a concatenated string argument to the system() function?
body: <p>How to pass a concatenated string argument to the <code>system()</code> function?</p>
  
  <ul>
  <li>I am using <code>GCC 4.5.2</code></li>
  <li>I have to make a <code>std::string</code> get executed in <code>shell</code> and to write its output
  to an external file.</li>
  <li>I use the <code>system()</code> function in order to perform this task.</li>
  </ul>
  
  <p>I couldn't perform the execution of this function since I could pass only <code>char*</code> arguments and no <code>std::string</code> arguments.</p>
  
  <p>Here is my code..</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  using namespace std;
  
  void getch()
     {
     int c;
     fflush(stdout);
     while ((c = getchar()) != '\n' &amp;&amp; c!= EOF) { /* Do Nothing */ }
     }
  
  int main()
  {
  string optTechniques[] = {
          "-fauto-inc-dec", "-fcprop-registers", "-fdce",
          "-fdefer-pop", "-fdelayed-branch", "-fdse",
          ........,   .....,  ......
           }
  string str;
  str="gcc -Wall "+optTechniques[i]+" -o output";
  cout&lt;&lt;"\n"&lt;&lt;str&lt;&lt;"\n";  /* Here it prints correctly */
  system(str);    /* This is generating error. */
  string val;
  system("sh execTime.sh");
  
  ifstream readFile("output.log");
  readFile&gt;&gt;val;  /* Works perfectly if execution is done. */
  float execTime=strtof(val.c_str(),NULL);    /* Converts the string to float */
  
  getch();
  
  cout&lt;&lt;"\nExecution Time: "&lt;&lt;execTime&lt;&lt;"\tTechnique: "&lt;&lt;optTechniques[i]&lt;&lt;"\n";
  return 0;
  }
  </code></pre>
  
  <p><strong>Update:</strong> <em>Thanks. I have got the answer.</em></p>