在c中执行与execvp的联系

时间:2016-05-01 11:53:17

标签: c linux

我想使用execvp在c中创建一个文件。该文件的名称在int1变量中。但它没有用。

int int1;
sscanf((char*) file_memory,"%d",&int1 );
char* arg_list[] = {
        "touch",
        "int1",
         NULL
        };
execvp ("touch",arg_list);

1 个答案:

答案 0 :(得分:1)

您正在创建一个名为int1的文件,因为您编写了一个字符串文字。变量不会在字符串文字中扩展。您需要使用整数值填充字符串变量。

int int1;
sscanf((char*) file_memory,"%d",&int1 );
char name[20];
snprintf(name, sizeof name, "%d", int1);
char *arg_list[] = {
    "touch",
    name,
    NULL,
};
execvp("touch", arg_list);