Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Linked Lists Up: Examples Previous: Using qsort, random numbers   Contents

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. */

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

Tim Love 2010-04-27