C编程 - 得到shell命令的输出

#include 

int main(void)
{
    FILE *file = popen("date", "r");
    if (file == NULL) {
        perror("popen");
        return 1;
    }
    char buffer[256] = { 0 };

    while (fgets(buffer, 255, file) != NULL) {
        printf("example->%s", buffer);
    }

    pclose(file);
    return 0;
}
$ gcc a.c && ./a.out
example->Tue May 29 16:26:46 CST 2018

你可能感兴趣的:(C编程 - 得到shell命令的输出)