繁体   English   中英

如何使用 execlp 将命令行参数传递给 C 程序

[英]How to pass command line arguments to C program using execlp

我正在尝试在 ac 程序中使用 execlp 来运行另一个 c 程序。 exec 函数确实调用了程序,但它没有正确传递整数参数。 我的执行电话是:

int exec_arg_1, exec_arg_2;

if(pid == 0){
    printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
    execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
       "philosopher.o", &exec_arg_1, &exec_arg_2, NULL );
           printf("Exec didn't work...\n");
    }

我将值分配给 exec_arg ints,并在之前打印它们以确保它们是正确的,但是 Philosopher.o 函数只是从该位置读取 0。 如果我从命令行运行 Philosophy.o,它会正常读取参数。

程序的参数总是字符串。

int exec_arg_1, exec_arg_2;

if (pid == 0){
    printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
    char arg1[20], arg2[20];
    snprintf(arg1, sizeof(arg1), "%d", exec_arg_1);
    snprintf(arg2, sizeof(arg2), "%d", exec_arg_2);
    execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
       "philosopher.o", arg_1, arg_2, NULL );
    fprintf(stderr, "Exec didn't work...\n");
    exit(1);
}

请注意, execlp()实际上只对固定数量的参数有用(或者,至少,当参数数量有一个小的固定上限时)。 大多数情况下, execvp()是更好的选择。

此页面包含大量使用示例....

编辑:从链接添加代码片段来自上面链接的代码片段

static void show_info_page(const char *git_cmd)
{
    const char *page = cmd_to_page(git_cmd);
    setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
    execlp("info", "info", "gitman", page, (char *)NULL);
    die(_("no info viewer handled the request"));
}

我认为最好的做法是首先查看execlp(3) 手册页

编辑:从手册页中添加了对 execlp(3) 的解释 FreeBSD 手册页解释了 execlp() 的用法如下

 int
 execlp(const char *file, const char *arg, ... /*, (char *)0 */);

 The initial argument for these functions is the pathname of a file which
 is to be executed.

 The const char *arg and subsequent ellipses in the execl(), execlp(), and
 execle() functions can be thought of as arg0, arg1, ..., argn.  Together
 they describe a list of one or more pointers to null-terminated strings
 that represent the argument list available to the executed program.  The
 first argument, by convention, should point to the file name associated
 with the file being executed.  The list of arguments must be terminated
 by a NULL pointer.

 The functions execlp(), execvp(), and execvP() will duplicate the actions
 of the shell in searching for an executable file if the specified file
 name does not contain a slash ``/'' character.  For execlp() and
 execvp(), search path is the path specified in the environment by
 ``PATH'' variable.  If this variable is not specified, the default path
 is set according to the _PATH_DEFPATH definition in <paths.h>, which is
 set to ``/usr/bin:/bin''

PS:一些信息,例如默认搜索路径,垫子根据您的系统而有所不同

您的问题是execlp的 arg 参数采用字符串指针而不是整数。 从联机帮助页

int execlp(const char *file, const char *arg, ...);

在将它们传递给 execlp 之前,您必须将它们转换为字符串。

#include<stdio.h>
#include<unistd.h>

#define MAXDIGITS 22

main()
{
    int exec_arg_1, exec_arg_2;

    char execStr1[MAXDIGITS + 1];
    char execStr2[MAXDIGITS + 1];

    exec_arg_1 = 750;
    exec_arg_2 = 25;

    snprintf(execStr1, MAXDIGITS + 1, "%d", exec_arg_1);
    snprintf(execStr2, MAXDIGITS + 1, "%d", exec_arg_2);

    printf("Our Strings: %s, %s\n", execStr1, execStr2);
    execlp("/home/drlight/Desktop/asp/Assignment_3/philosopher.o", "philosopher.o", execStr1, execStr2, NULL);
}

您需要确保 MAXDIGITS 足够大以容纳您的数字的所有十进制数字,但在大多数当前平台上,即使是多头,25 也应该足够了。 但是请记住,在未来版本的 gcc 和/或不同的编译器中,这可能会有所不同。 也不要忘记为负面留下空间。 您可以通过导入limits.h 并打印INT_MAX 和LONG_MAX 的值来检查这些最大值。

#include<stdio.h>
#include<limits.h>

main(int argc, char * argv[])
{
    printf("Int max: %d\n", INT_MAX);
    printf("Long max: %ld\n", LONG_MAX);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM