在Unix或者Linux系统上执行一个命令,输出会默认打印在标准输出上。下面的程序通过system系统调用执行一个ifconfig命令,然后将输出重定向到一个txt文件当中。

#include <stdout.h>

#include <stdlib.h>

#include <unistd.h>

#define BUF_MAX 40

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

{

  // 设置要重定向的目的文件

  char dfile[] = "/root/test.txt";

  // 要执行的命令

  char cmd[] = "ifconfig";

  // 将文件指针与标准输出流绑定

  FILE *fd = freopen(dfile, "w+", stdout);

  // system系统调用执行ifconfig

  system(cmd);

  // 关闭文件指针fd

  fclose(fd);

  return 0;

}

相关文章:

  • 2021-08-17
  • 2022-12-23
  • 2021-12-25
  • 2021-12-22
  • 2022-12-23
  • 2021-08-17
  • 2021-06-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-09
  • 2021-12-12
  • 2022-12-23
  • 2021-05-28
  • 2021-07-10
  • 2022-12-23
相关资源
相似解决方案