title: Retrieve ping time from system command in c++ (ping x.x.x.x -n)
body: <p>I'm write a small console application (windows plateform using visual studio 2012) that show the current ping to an IP and some others stats. (average of the last 100pings, packet loss, etc...)
  Here is my actual implementation:</p>
  
  <pre><code>char buffer[100];
  FILE *fp = _popen("ping -l 1 -n 1 61.5.222.121" , "r");
  for (i=0;i&lt;8;i++) {
     fgets(buffer,100,fp);
  }
  _pclose(fp)
  </code></pre>
  
  <p>I call this function in my main with: </p>
  
  <pre><code>_beginthread( ping, 0, NULL );
  </code></pre>
  
  <p>Then I extract the ping from buffer with strstr and sscanf_s.
  But when I execute the program, I see in the process manager that ping.exe and cmd.exe keeps opening/closing, which is normal because at each popen, it launch one.</p>
  
  <p>I'm looking for a way to have a cleaner way of retrieving the ping time value. For example something that can read the output each time a new line is created in the popen (with the command "ping google.com -t"), this way there would be only one ping.exe and cmd.exe process that make all the job and the ping refresh time would be much more fast.</p>
  
  <p>I've looked into the IcmpSendEcho command, but this looks very complicated for me. I basically want a simple function that runs into the background and write the ping response time in a global variable each time he recieve a new one.</p>