[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Linked Lists Up: Examples Previous: Using qsort, random numbers

Calling other programs

The commands used from the command line can be called from C.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
FILE *popen();
FILE *fp;
char string[32];
  /* First use the system() call. Output will go to stdout. */
  system("date");

  /* Now `capture' the output of date using popen() */
  fp = popen("date","r");
  if (fp == NULL)
    fprintf(stderr,"Cannot run date\n");
  else{
    fgets(string, 32, fp);
    printf("The date command returns [%s]\n", string);
    pclose(fp);
  }
}



Tim Love
1999-10-06