请问EXECVP系统调用是否支持IO重定向

Will EXECVP system call supports IO redirections

EXECVP系统调用是否支持IO重定向

这意味着这是否给出了所需的输出

char *args[]={"cat","A.txt","B.txt",">","C.txt",NULL};
execvp(args[0],args);

我的意思是 A.txt 和 B.txt 中的数据会转到 C.txt

如果没有,为什么?

UPD : 有两个疑惑在评论中请大家说明一下

从技术上讲,这不是您问题的答案,您的问题已在评论中得到解答。但是解释了如何使用 execvp

进行重定向

当用execvp启动一个新程序时,它将继承当前的文件描述符。因此,如果您设置文件描述符 1(用于标准输出) 在调用 execvp 之前重定向到“C.txt”,新程序将 写入“C.txt”:

// Open "C.txt" for writing, creating it if it doesn't exist, or
// clearing its content if it does exist.
// `mode` should be set as appropriate 
//
int fd = creat("C.txt", mode);
if (fd == -1)
{
   // Swap out error handling to suit your needs
   perror("open failed");
   exit(EXIT_FAILURE);
}

// We want new process to have "C.txt" on file descriptor 1
if (dup2(fd, 1) == -1)
{
   perror("dup failed");
   exit(EXIT_FAILURE);
}


// "C.txt" is now writable on file descriptor 1, so we don't need the
// old one. Actually, the old one could confuse the new executable.
close(fd);


// We can now execute new program. It will inherit current open 
// file descriptors and it will see "C.txt" on file descriptor 1
char *args[]={"cat","A.txt","B.txt",NULL};
execvp(args[0],args);

// If we reach this point, `execvp` failed.
perror("execvp failed");
exit(EXIT_FAILURE);