Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们可以在c中的execvp()中将变量作为命令行参数传递吗_C_Execvp - Fatal编程技术网

我们可以在c中的execvp()中将变量作为命令行参数传递吗

我们可以在c中的execvp()中将变量作为命令行参数传递吗,c,execvp,C,Execvp,有人能告诉我吗 是否可以将变量作为命令行参数传递给c中的execvp()。 如果是,如何通过? 调用可执行文件时是否有其他方法传递变量 我的代码片段是 struct sample { int links[100],nodes[100],hosts[100],linkcount,nodecount,hostcount; }; int main() { pid_t process; struct sample s1; //assigned values to al

有人能告诉我吗 是否可以将变量作为命令行参数传递给c中的execvp()。 如果是,如何通过? 调用可执行文件时是否有其他方法传递变量

我的代码片段是

struct sample
{
    int links[100],nodes[100],hosts[100],linkcount,nodecount,hostcount;
};

int main() 
{
    pid_t process;
    struct sample s1;
    //assigned values to all the structure members.
     const char* command ="./abc";
    process=fork();
    if(process < 0)
    {
        perror("fork failure");
        exit(1);
    }
    if(process == 0)
    {
        execvp(command,NULL);/*Can I pass entire structure as commandline argument in execvp(command,&s1,NULL)*/
    }
    else
    {
        wait(NULL);
        printf("child is dead..parent process is resuming"); 
     }
     return 0;
}
struct示例
{
int links[100],节点[100],主机[100],linkcount,nodecount,hostcount;
};
int main()
{
pid_t过程;
结构样本s1;
//为所有结构构件指定了值。
const char*command=“/abc”;
进程=fork();
如果(进程<0)
{
perror(“分叉故障”);
出口(1);
}
如果(进程==0)
{
execvp(command,NULL);/*我可以在execvp(command,&s1,NULL)中将整个结构作为命令行参数传递吗*/
}
其他的
{
等待(空);
printf(“子进程已死亡..父进程正在恢复”);
}
返回0;
}
您可以(使用execl()),但它必须是字符串(
常量字符*
)。要将
int
转换为字符串,请使用
itoa()
。然后您可以像这样执行
/abc 10

execl(command, command, itoa(s1.id, 10), NULL);
itoa
的第二个参数是基础

如果必须使用
execvp()
,则需要一个字符串数组,其中NUM_ARGS是参数数:

char **args=malloc(sizeof(char*)*NUM_ARGS);
args[0]=itoa(s1.id, 10);
execvp(command, args);

对。只需创建一个保存参数字符串的buf和一个argv,argv是指向buf中参数的指针数组。以下是一段代码片段:

char *argv[128];
char buf[8192];   // Each argument string in this buffer is null-terminated.    
int argc = 0;

// ...

// The first argument is the command, so we copy it to buf and make it pointed by argv[0].
const char* command ="./abc";
strcpy(buf, command);   
argv[argc++] = buf;     
buf += strlen(command) + 1;

// The second argument will be saved after the first one in buf and is pointed by argv[1].
char * arg = itoa(s1.id, 10);
strcpy(buf, arg);
argv[argc++] = buf;

// Required.
argv[argc] = NULL;

// ...

// Then you can call execvp!
execvp(command, argv);
请注意,此代码段不安全。更多详情请参考乔纳森·莱夫勒(Jonathan Leffler)的评论


有关更多详细信息,请参见。

或更好的
s(n)printf
,尤其是当您需要额外格式时。好的。谢谢。我可以将结构变量s1传递给execvp()而不是将其成员作为execvp(命令,s1)传递吗?如果是这样,那么如何在abc中类型转换和使用该结构变量。c@theunamedguy@bhargavi:这相当困难。请注意,
execl()
需要一个空指针(
char*
)来标记参数列表的结尾。另外,
itoa()
值变为
argv[0]
。虽然你可以通过这种方式传递论点,但这不是常规的方式;您将“程序名”作为第一个参数传递,然后是
itoa(s1.id,10)
,然后是一个空指针以指示参数的结束。@bhargavi:命令的所有参数都只是字符串。您必须将要传递给命令的所有内容转换为字符串,并将指向这些字符串的指针放入
argv
数组中。记住null终止指针数组,数组的第0个元素(通常)是程序名。您通常应该能够编写
execvp(argv[0],argv)其中变量的名称是可协商的,但程序的名称应该是参数数组的第一个元素。请注意,使用第二个参数的空指针调用
execvp()
,会调用未定义的行为;您的代码可能会崩溃。
“/abc\0”
符号在字符串末尾有一个无意义的第二个null。由于
strcpy()
null终止字符串,因此
*buf='\0'分配是不必要的。如果您保留了它,您还可以将它与
buf++
组合为
*buf++='\0'。理论上,在复制之前应该检查命令的长度,以防止缓冲区溢出。大致来说,如果不能使用
memmove()
复制字符串,则不知道复制字符串是否安全。好啊我同意放弃strcpy()
是激进的,但记住这个想法。@JonathanLeffler非常感谢你!我根据你的评论做了一些编辑。