簡體   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