我想从C文件运行execlp()并将结果写入某个输出文件。
我用的台词是:

buff = "./cgi-bin/smth";
execlp(buff, buff, "> /cgi-bin/tmp", NULL);

其中smth是编译的c脚本。
但是smth打印到stdout,没有文件出现。
会发生什么,以及如何将脚本结果放入输出文件?

最佳答案

如果使用dup2,则必须使用execlp自行处理。相比之下,你可以看看我如何处理execvp文件。我传递一个向外重定向的标志,然后处理它:

  if (structpipeline->option[0] == 1) { /* output redirection */
        int length = structpipeline[i].size;
        char *filename = structpipeline->data[length - 1];
        for (int k = length - 2; k < length; k++)
            structpipeline->data[k] = '\0';
        fd[1] = open(filename, O_WRONLY | O_CREAT, 0666);
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);
    } /* TODO: input redirection */
    execvp(structpipeline[i].data[0], structpipeline[i].data);

另见这个问题
Redirecting exec output to a buffer or file

关于c - C:execlp()和>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37426941/

10-11 21:27