本文介绍了处理execvp的参数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我称之为 execvp ,例如 execvp(回声,B)其中B是参数数组该命令,将改变这个数组后影响做出previously的execvp调用?当我尝试调用execp(回声,B),它最终打印出(空)来代替的B里面的内容。任何人都可以指出我为什么,我必须做正确传递参数呢?

When I call execvp, for example execvp(echo, b) where b is an array of arguments for the command a, will changing this array later affect the execvp call made previously? When I try calling execp(echo, b), it ends up printing out (null) instead of the content inside of b. Can anyone point out why and what I have to do to pass the arguments correctly?

推荐答案

在调用执行exec()或一个,如果其亲属,原来的程序不会的存在的了。这意味着什么在该程序中可能会影响执行exec()电话后任何事情,因为它永远不会运行。也许你没有正确建立您的参数数组?这里的 execvp的快速工作示例()

After you call exec() or one if its relatives, your original program doesn't exist anymore. That means nothing in that program can affect anything after the exec() call, since it never runs. Maybe you're not building your arguments array correctly? Here's a quick working example of execvp():

#include <unistd.h>

int main(void)
{
  char *execArgs[] = { "echo", "Hello, World!", NULL };
  execvp("echo", execArgs);

  return 0;
}

从:

execv() execvp() execvpe() 函数提供指针数组为空结尾的字符串重新present参数列表提供给新程序。第一个参数,按照惯例,应该指向与正在执行的文件相关联的文件名。指针数组的必须的一个 NULL 指针被终止。

一个常见的​​错误是跳过关于部分的第一个参数,按照惯例,应该指向与正在执行的文件相关联的文件名。这使得确定回声被回声为的argv [0] ,其中psumably $ P $它所依赖的一部分上。

A common mistake is to skip the part about "The first argument, by convention, should point to the filename associated with the file being executed." That's the part that makes sure echo gets "echo" as argv[0], which presumably it depends on.

这篇关于处理execvp的参数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:30