[system]c/c++调用cat命令

因为cat并不是一个带返回的命令,而是输出到标准输出。所以使用system("cat /sys/class/gpio/...."); 得不到我们想要的结果. 下面的/bin/ls同/bin/cat。

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


int main( int argc, char *argv[] )
{

  FILE *fp;
  char path[1035];

  /* Open the command for reading. */
  fp = popen("/bin/ls /etc/", "r");
  if (fp == NULL) {
    printf("Failed to run command
" );
    exit(1);
  }

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
    printf("%s", path);
  }

  /* close */
  pclose(fp);

  return 0;
}
原文地址:https://www.cnblogs.com/aaronLinux/p/6944966.html