Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dos commands and C

Status
Not open for further replies.

Ken77

Programmer
Sep 10, 2002
4
IE
How do I execute DOS commands in C? - I want to run netstat from my program and write the results to a log.

Thanks,

K.
 
How about this?
Code:
#include <stdio.h>

int
main (int argc, char *argv[])
{
    if (system(&quot;netstat > /path/to/logfile&quot;) != 0)
    {
        printf(&quot;netstat failed.\r\n&quot;);
        return 1;
    }
    return 0;
}
//Daniel
 
God, I'm such am amature!

Thanks daniel - your timing couldn't have been better.

K.
 
God, I'm such an amature!

Thanks Daniel - your timing couldn't have been better.

K.
 
Isn't the #include <stdlib.h> required when using system() ?
 
an alternative, do you have 'popen()' ???

FILE *fd;
FILE *out;

if(!(out = fopen(&quot;logfilename&quot;,&quot;w&quot;))) exit(1);
fd = popen(&quot;command-to-exec 2>&1&quot;,&quot;r&quot;);
while(fgets(buff,sizeof(buff),fd)) fprintf(out,&quot;%s&quot;,buff);
pclose(fd);
fclose(out); vox clamantis in deserto.
 
Yes, stdlib.h is required for the system function. Sorry about that [blush]. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top