The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 05-12-2008
gautamdheeraj gautamdheeraj is offline
Registered User
 

Join Date: Feb 2008
Posts: 18
you can use popen following way:

Code:
#include <stdio.h>
int main()
{
#define BUFSIZE 1024
char buf[1024];
FILE *fp = NULL;
char *cmd = "ls -al /home/";
int nbytes = 0;
int fbytes = BUFSIZE;
int ch =0;
fp = popen(cmd, "r");
if (fp == NULL)  {
        printf("unable to open %s", cmd);
        exit(0);
}

while (nbytes < fbytes && (ch = fgetc(fp)) != EOF)
        buf[nbytes++] = ch;
buf[nbytes] = 0;
pclose(fp);
printf("output for %s %d:\n %s", cmd , nbytes, buf);
return 0;
}
Reply With Quote