473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Argv

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.

Apr 11 '06 #1
25 5860
<br****@gmail.c om> schrieb im Newsbeitrag news:11******** **************@ g10g2000cwb.goo glegroups.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] = somePointerToNo nConstCharacter (s);
argv[numberOfStrings] = 0;

should help.

Heinz
Apr 11 '06 #2
i think this can work:

char **array;
array = malloc(sizeof(c har *) * NUMBER_OF_STRIN GS);
-------or-------
char *array[MAX_NUMBER_OF_S TRINGS]; (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_STRIN GS; i++)
{
fgets(buffer, 1024, stdin); // get the ith string
array[i] = malloc(sizeof(b uffer)); // create new string in the
array
strcpy(array[i], buffer); // assign the string
}

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

does it work?

Apr 11 '06 #3
Ops sorry, i forgot this was the c.l.c++, i replied in C :] Sorry!

Apr 11 '06 #4
??
perhalps need a argc to count the size
<br****@gmail.c om>
??????:11****** *************** *@g10g2000cwb.g ooglegroups.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.

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.

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

char** vector_to_valis t (const std::vector<std ::string>& vect)
{
std::auto_ptr<c har*> valist (new char* [vect.size()]);
std::transform (vect.begin(), vect.end(),
const_cast<cons t char**> (valist.get()),
std::mem_fun_re f (&std::string:: c_str));
return valist.release( );
}

theres some const breakage going on here, but the exec family doesn't
mod the strings anyway.
Apr 11 '06 #6
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
br****@gmail.co m wrote:
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.
From unistd.h right?
I will not know the amount of character arrays (arguments) until
runtime, so I need to use new or calloc to get this done.
No, don't use calloc. Don't ever use calloc in a C++ program.
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()];
The problem with that is that both argv and envp must contain modifiable
strings (char* not const char*.)
return something that will pass into the above function.

Sorry if this seems weird.

struct extract_buffer : unary_function< char*, vector<char> > {
char* operator()( vector<char>& vec ) const {
return &vec[0];
}
};

typedef vector< vector< char > > RaggedArray;

int execve( const char* filename, RaggedArray& argv,
RaggedArray& envp ) {
vector<char*> argv_guts;
transform( argv.begin(), argv.end(), back_inserter( argv_guts ),
extract_buffer( ) );
vector<char*> envp_guts;
transform( envp.begin(), envp.end(), back_inserter( envp_guts ),
extract_buffer( ) );
return execve( filename, &argv_guts[0], &envp_guts[0] );
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 11 '06 #7
pillbug wrote:

I've successfully parsed all the arguments into strings, and placed
them in a nice vector. (Probably an unnecessary step, but hey)
char** vector_to_valis t (const std::vector<std ::string>& vect)
{
std::auto_ptr<c har*> valist (new char* [vect.size()]);


auto_ptr should not control an array returned by new[], because the result
of calling delete (without []) on it is undefined.
std::transform (vect.begin(), vect.end(),
const_cast<cons t char**> (valist.get()),
std::mem_fun_re f (&std::string:: c_str));
return valist.release( );
}

theres some const breakage going on here, but the exec family doesn't
mod the strings anyway.


I don't trust that const_cast. Why not use vector<char*> valist, then get
its first element's address with &valist[0]?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 11 '06 #8
pillbug wrote:
char** vector_to_valis t (const std::vector<std ::string>& vect)
{ .... return valist.release( );
}


I forgot to notice; that return value is just wrong. If you need a
vector<char*>, then return one. Or better vector<string>. Return it by
copy.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 11 '06 #9

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


No, don't use calloc. Don't ever use calloc in a C++ program.


why? :-)
std::calloc() is quite useful in some cases

Apr 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2627
by: Robin Sanderson | last post by:
Sorry in advance if this is a stupid question - I am new to C++. In the process of converting program to be run from the command line into a function to be run from another program I noticed behaviour that I do not understand. Consider the example programs below: Program 1 below is a simple program that merely outputs the command line arguments. This compiles and runs fine with Microsoft Visual C++ 6.0 and g++ 3.3.1.
9
5933
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the letter "d" this is what i have so far if (argv) { write_read_input_file(filename); }
28
12617
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one level3 argument for each level2 argument, etc. Suppose I code it similar to this fragment: int main( int argc, char *argv ) {
5
3990
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
32
8622
by: mnaydin | last post by:
Assume the main function is defined with int main(int argc, char *argv) { /*...*/ } So, is it permitted to modify the argv array? The standard says "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program,". According to my reading of the standard, for example, ++argv and ++argv are both permitted, but not ++argv because it says nothing about the argv array itself. Is my...
22
2206
by: Joe Smith | last post by:
It is nothing short of embarrassing to feel the need to ask for help on this. I can't see how I would make the main control for this. What I want is a for loop and a test condition. And while I know, from things I pondered 2 decades ago, that a fella can write code without a goto, I'm stuck. /* sieve1.c */ #define whatever 20 #define N whatever
4
9804
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of data that I get in argv ? Q2. what is encoding of string constants defined in programs (for example L"--count") ?
11
6067
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
4
7188
by: Romulo Carneiro | last post by:
Hi, I'm programming in Windows XP and i'm trying to get all arguments of some application, but i only have gotten five argv. When i put more then five(5), it didn't display. =>Input Command Line: Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6 =>output of my program: Size of Argc:8 Size of Argv: 4
12
6635
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error that if I do this: #include <stdio.h> int main(int argc, char *argv) {
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.