title: parsing command-line arguments from proc/pid/cmdline
body: <p>I'm trying to parse the command-line arguments of <strong>another program</strong> (which is an emulator) inside my program using <code>system()</code> command and the emulator's <code>pid</code>. Unfortunately using both file read and <code>cat</code>, the output is not well-formatted, so I can not really get the data. <code>cat</code> on  command line shows the file contents with spaces removed, the whole string sticked together, and using <code>ifstream</code>, it only shows the name of the program (the first argument I guess). Anyone has got any ideas? </p>
  
  <p>The format of the arguments is like this: </p>
  
  <pre><code>sudo ./src/yse6 -w -f tracefiles/capacity.3Mbps_400RTT_PER_0.0001.txt -at=eth1 -an=eth0
  </code></pre>
  
  <p>and eventually I need to show the same string as formatted above.</p>
  
  <p>Here is what I've done so far: (<code>ExecCommand()</code> gets a command, runs it in command-line and returns the result as <code>string</code>. <code>Secondary()</code> tries to get the file contents using file reader.)</p>
  
  <pre><code>std::string ExeCommand(const char* cmd) {
      FILE* pipe = popen(cmd, "r");
      if (!pipe) return "ERROR";
      char buffer[128];
      std::string result = "";
      while(!feof(pipe)) {
          if(fgets(buffer, 128, pipe) != NULL)
              result += buffer;
      }
      pclose(pipe);
      return result;
  }
  
  void secondary(string addr){
  
          ifstream file(addr.c_str(),ios::in);
          if (file.good())
          {
              string str;
              while(getline(file, str))
              {
                  istringstream ss(str);
                  cout&lt;&lt;str&lt;&lt;endl;
                  char num[50];
                  while(ss &gt;&gt; num)
                  {
                      cout&lt;&lt;num;
                  }
              }
          }else{
              cout&lt;&lt;"no file exists."&lt;&lt;endl;
          }
  }
  
  int main (int argc, char* argv[])
  {
      if ((string) argv[1] == "-q") {
          string pid=ExeCommand("ps -A | grep 'yse6' | awk '{print $1}'");
          if(pid==""){
              cout&lt;&lt;"No YSE emulator is running."&lt;&lt;endl;
          }else{
              pid=pid.substr(0,pid.size()-1);
              cout&lt;&lt;pid&lt;&lt;endl;
              string addr="cat /usr/bin/strings /proc/"+pid+"/cmdline";
  
              cout&lt;&lt;addr&lt;&lt;endl;
  //          secondary(addr);
  
              const char * c = addr.c_str();
              string config=ExeCommand(c);
              //get the config
              cout &lt;&lt; config&lt;&lt;endl;
          }//end of else
      }
  }
  </code></pre>