stdout文件描述符设置不正确

时间:2018-04-03 07:59:38

标签: stdout file-descriptor

我正在调用IBM API,QzshSystem。 其先决条件是确保文件描述符0,1和2可用。 我的代码看起来像这样,

  int fd0 = -1, fd1 = -1, fd2 = -1;

  if (fcntl(0, F_GETFL) == -1)
  {
     fd0 = open("/dev/null", O_RDONLY);
     if (fd0 == -1) 
     {
       fprintf(stderr, "Error %d opening file /dev/null\n", errno);
     }
  }
  if (fcntl(1, F_GETFL) == -1)
  {
     fd1 = open("/apifile.out", O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
     if (fd1 == -1)
     {
        fprintf(stderr, "Error %d opening file %s\n", errno, "/apifile.out");
     }
  }
  if (fcntl(2, F_GETFL) == -1)
  {
     fd2 = open("/apifile.err", O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
     if (fd2 == -1)
     {
        fprintf(stderr, "Error %d opening file %s\n", errno, "/apifile.err");
     }
  }

然后在传递参数的某些格式后调用API,

  aReturn = QzshSystem(CmdStrng);

API不起作用(它应该运行shell cmd),并返回错误8;从其文档中说明stdout文件描述符1未正确设置。

这怎么可能?我是否应该使用close()和open?

来修复stdout的文件描述符

0 个答案:

没有答案
相关问题