In posix systems you can do what you want with popen()
.
Possibly there are other specific ways for specific operating systems (Windows, Android, iOS, MINIX, Nextstep, ...)
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char info[1000000]; // 1 Mbytes deve chegar :-)
size_t infolen = 0;
char linha[1000];
size_t linhalen;
char comando[] = "man fprintf";
FILE *processo;
processo = popen(comando, "r");
if (!processo) {
perror("popen");
exit(EXIT_FAILURE);
}
while (fgets(linha, sizeof linha, processo)) {
linhalen = strlen(linha);
if (infolen + linhalen >= sizeof info) {
fprintf(stderr, "O output tem muitas linhas.\n");
exit(EXIT_FAILURE);
}
strcpy(info + infolen, linha);
infolen += linhalen;
}
pclose(processo);
// usa info, por exemplo imprime os 6 primeiros caracteres
printf("%.6s", info);
return 0;
}
The
man fprintf
basic has many control characters in output. You can filter these characters with the programcol
:man fprintf | col -b
– pmg