c-无法理解execvp()参数的格式

  • 本文关键字:参数 格式 execvp c exec
  • 更新时间 :
  • 英文 :


我目前正在处理一个需要使用execvp的项目。我在使用这个时遇到了麻烦,因为我无法让它发挥作用,所以也许这与我传递论点的方式有关
我需要执行教授提供给我们的程序
执行此程序的参数是:
$ ./bin/aurrasd-filters/aurrasd-gain-double < samples/sample-1-so.m4a > output.m4a
这就是我试图设置参数以使其工作的方式:

int main () {
char *agr2[] = {"./bin/aurrasd-filters/aurrasd-gain-double", "<", "samples/sample-1-so.m4a", ">", "output.m4a", NULL};
if (!fork()) {
execvp(*agr2, agr2);
}
else {
wait(NULL);
printf("Terminated"n);
}
return 0;
}

这会把所有的论点都放在正确的位置吗?我似乎不知道错误在哪里。

我需要做的是重定向stdout和stdin。<>并不像我想的那样是可执行文件的参数
它看起来像这样:

int main () {
char *arg[] = {"./bin/aurrasd-filters/aurrasd-gain-double", NULL};
char *input = "samples/sample-1-so.m4a";
char *output = "output.m4a";
if (!fork()) {
int input_f;
if ((input_f = open(input, O_RDONLY)) < 0) {
perror("Error opening input file");
return -1;
}
dup2(input_f, 0);
close(input_f);

int output_f;
if ((output = open(output, O_CREAT | O_TRUNC | O_WRONLY)) < 0) {
perror("Error creating output file");
return -1;
}
dup2(output_f, 1);
close(output_f);
execvp(*arg, arg);
_exit(0);
}
else {
wait(NULL);
}
return 0;
}

如上所述,我们需要处理I/O重定向,我使用dup2()函数来完成此操作,将输入文件设置为stdin,创建一个输出文件并将其设置为stdout。这对我很有效,也许将来会对某人有所帮助。。

最新更新