c関数でshellコマンドを実行する




 c     shell             

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int ret = -1;
    FILE* pf;
    /* Can not get the output of the cmd */
    ret = system("ls /dev/loop* | wc -l");

    printf("ret = %d
", ret); /* This mothed can get the output of the cmd */ pf = popen("ls /dev/loop* | wc -l", "r"); fscanf(pf, "%d", &ret); printf("ret = %d
", ret); pclose(pf); return 0; }