ARGV [英] Argv

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

问题描述

我正在尝试创建一个字符数组数组。我会使用字符串,

但是我需要输入的函数看起来像这样


int execve(const char * filename,char * const argv [] ,char * const

envp [])。我知道这不是一个C ++函数,我只询问如何创建像这样的函数实际可以接受的东西。


我不会知道字符数组(参数)的数量,直到

运行时,所以我需要使用new或calloc来完成这个。


我已成功将所有参数解析为字符串,并将它们放在一个漂亮的向量中。 (可能是一个不必要的步骤,但是嘿)


所以类似


make charray;

for(int i = 0; i< vector.size(); i ++){


chararray [i] [vector [i] .c_str()];


}


返回会传递给上述功能的内容。


如果这看起来很奇怪,很抱歉。


感谢阅读。


一个疲惫的java人。

解决方案

<峰; br **** @ gmail.com> schrieb im Newsbeitrag新闻:11 ********************** @ g10g2000cwb.googlegr oups.com ...

我是试图创建一个字符数组数组。我会使用字符串,
但是我需要输入的函数看起来像这样的内容(const char * filename,char * const argv [],char * const
envp [])。我知道这不是一个C ++函数,我只询问如何创建像这样的函数实际可以接受的东西。




函数预期两个指向char的指针数组,而不是字符数组数组。类似


char ** argv = new char * [numberOfStrings + 1];

for(int i = 0; i< numberOfStrings; ++ i)

argv [i] = somePointerToNonConstCharacter(s);

argv [numberOfStrings] = 0;


应该有帮助。


Heinz


我认为这可行:


char ** array ;

array = malloc(sizeof(char *)* NUMBER_OF_STRINGS);

-------或-------

char * array [MAX_NUMBER_OF_STRINGS]; (如果你不喜欢这里的malloc :)


那么:

int i;

char buffer [1024 ];

fgets(char * s,int size,FILE * stream);

for(i = 0; i< NUMBER_OF_STRINGS; i ++)

{

fgets(buffer,1024,stdin); //得到第i个字符串

array [i] = malloc(sizeof(buffer)); //在

数组中创建新字符串

strcpy(array [i],buffer); //分配字符串

}


execve(blah,array,blah); //传递数组


它有用吗?


对不起,我忘了这是clc ++ ,我在C回复:]抱歉!


I''m trying to create an array of character arrays. I would use strings,
but the function I need to feed into looks like this

int execve(const char *filename, char *const argv [], char *const
envp[] ). I know this is not a C++ function, and I only ask for how to
create something that a function like this will actually accept.

I will not know the amount of character arrays (arguments) until
runtime, so I need to use new or calloc to get this done.

I''ve successfully parsed all the arguments into strings, and placed
them in a nice vector. (Probably an unnecessary step, but hey)

so something like

make charray;
for(int i =0; i< vector.size(); i++){

chararray[i][vector[i].c_str()];

}

return something that will pass into the above function.

Sorry if this seems weird.

Thanks for Reading.

A tired java person.

解决方案

<br****@gmail.com> schrieb im Newsbeitrag news:11**********************@g10g2000cwb.googlegr oups.com...

I''m trying to create an array of character arrays. I would use strings,
but the function I need to feed into looks like this

int execve(const char *filename, char *const argv [], char *const
envp[] ). I know this is not a C++ function, and I only ask for how to
create something that a function like this will actually accept.



The function expects two arrays of pointers to char, not "arrays of character arrays". Something like

char** argv = new char*[numberOfStrings + 1];
for (int i = 0; i < numberOfStrings; ++i)
argv[i] = somePointerToNonConstCharacter(s);
argv[numberOfStrings] = 0;

should help.

Heinz


i think this can work:

char **array;
array = malloc(sizeof(char *) * NUMBER_OF_STRINGS);
-------or-------
char *array[MAX_NUMBER_OF_STRINGS]; (if you don''t like a malloc here :)

then:
int i;
char buffer[1024];
fgets(char *s, int size, FILE *stream);
for (i = 0; i < NUMBER_OF_STRINGS; i++)
{
fgets(buffer, 1024, stdin); // get the ith string
array[i] = malloc(sizeof(buffer)); // create new string in the
array
strcpy(array[i], buffer); // assign the string
}

execve(blah, array, blah); // pass the array

does it work?


Ops sorry, i forgot this was the c.l.c++, i replied in C :] Sorry!


这篇关于ARGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆