I'm trying to store the number of page faults into an array in my c program. I want to execute bash command and store the output of it in an array. here's my command
$ cat /proc/vmstat | grep pgfault
A simple start, can be embellished and 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);
/* do something with lines[0 .. n-1] here */
/* then free them */
popen()plusfread().