This question already has answers here:
How can I run an external program from C and parse its output?
(8个答案)
6年前关闭。
我实际上希望在Linux上使用C程序在.txt文件中获取所有网络参数,例如IP地址,DNS服务器等。
(8个答案)
6年前关闭。
我实际上希望在Linux上使用C程序在.txt文件中获取所有网络参数,例如IP地址,DNS服务器等。
最佳答案
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;
}
10-06 04:53