将execlp输出重定向到文件

mir*_*raj 2 c redirect

如何将execlp输出重定向到文件?例如,我想将输出重定向execlp("ls", "ls", "-l", NULL)到输出文件(例如a.txt).

R..*_*R.. 5

你需要做这样的事情:

int fd = open("output_file", O_WRONLY|O_CREAT, 0666);
dup2(fd, 1);
close(fd);
execlp("ls", "ls", "-l", (char *)0);
Run Code Online (Sandbox Code Playgroud)