Advertisement
metalx1000

Save output of system command to variable with popen

Jan 31st, 2016
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main( int argc, char *argv[] )
  6. {
  7.  
  8.   FILE *fp;
  9.   char path[1035];
  10.  
  11.   /* Open the command for reading. */
  12.   fp = popen("/bin/ls /etc/", "r");
  13.   if (fp == NULL) {
  14.     printf("Failed to run command\n" );
  15.     exit(1);
  16.   }
  17.  
  18.   /* Read the output a line at a time - output it. */
  19.   while (fgets(path, sizeof(path)-1, fp) != NULL) {
  20.     printf("%s", path);
  21.   }
  22.  
  23.   /* close */
  24.   pclose(fp);
  25.  
  26.   return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement