Quantcast
Channel: User trojanfoe - Stack Overflow
Viewing all articles
Browse latest Browse all 226

Answer by trojanfoe for Is there a way to obtain the output of a linux command(like ifconfig) on a .txt file using a C program?

$
0
0

You want popen(). Here's an example, taken from... (see below).

void get_popen_data() {    FILE *pf;    char command[COMMAND_LEN];    char data[DATA_SIZE];    // Execute a process listing    sprintf(command, "ps aux wwwf");     // Setup our pipe for reading and execute our command.    pf = popen(command,"r");     if(!pf){      fprintf(stderr, "Could not open pipe for output.\n");      return;    }    // Grab data from process execution    fgets(data, DATA_SIZE , pf);    // Print grabbed data to the screen.    fprintf(stdout, "-%s-\n",data);     if (pclose(pf) != 0)        fprintf(stderr," Error: Failed to close command stream \n");    return;}

Edit: I have removed the link to the original source code as it looks like a domain change has occurred and the site now contains some nasty looking malware.


Viewing all articles
Browse latest Browse all 226

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>