operating system - execute bash commands in a c file and store the output in an array -
i'm trying store number of page faults array in c program. want execute bash command , store output of in array. here's command
$ cat /proc/vmstat | grep pgfault
a simple start, can embellished , improved...
char * lines[2000]; /* 2000 lines enough? */ int n = 0; file * fp = popen("your command here", "r"); if (fp == null) abort(); lines[0] = malloc(1000); /* 1000 byte lines enough? */ while ((fgets(lines[n], 1000, fp) != null) { if (n == 1999) abort(); /* oh crud... */ lines[++n] = malloc(1000); } free(lines[n]); pclose(fp);
Comments
Post a Comment