pinging with C system()

February 10, 2010 at 16:56:49
Specs: Windows Vista
hey when you put in something like

system(ping www.example.com);

you don't get any visible results...
anybody know if there is a way to print out the ip address it gets from the ping onto the screen.


See More: pinging with C system()

Report •

#1
February 10, 2010 at 21:00:59
If you want to keep the code portable, then you can redirect the output to a file...

char buf[BUFSIZ] = { 0 };
sprintf(buf, "ping www.example.com > ping.txt");
system(buf);

More than likely, you'll need to pause your app before you'll be able to read the file. You can make a crude timer function by including time.h

void snooze(int sec) {

   time_t st = time(NULL), et;
   
   do { et = time(NULL);
   } while(et < st + sec);
}

lastly, read the file back

FILE *fp;

// ping the site
// pause the app
// read back the file

if((fp = fopen("test.txt", "r")) != NULL) {

      memset(buf, 0, sizeof buf);

      while((fgets(buf, sizeof buf, fp)) != NULL) {
         char *p = strchr(buf, '\n');
         if(p != NULL) { *p = '\0'; }
         puts(buf);
      }

      fclose(fp);
}

I'm sure there are other ways, but that's what I tend to do. :P

HTH


Report •

#2
February 11, 2010 at 12:18:40
thanks :)

Report •

#3
February 11, 2010 at 14:35:04
Or you just do :

system("ping www.example.com");


??


Report •
Related Solutions


Ask Question