?
Solved

Passing int in execl

Posted on 2005-03-03
9
Medium Priority
?
297 Views
Last Modified: 2012-06-22
I need to pass an int (30) to be used in program called by execl.

for example:

execl("progb", "30", NULL);

main in progb looks like this:

int main(int argc, char *argv[])

I thought I could do the following but it doesn't work:  

int counter = atoi(argv[1]);
0
Comment
Question by:jma2000
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13456142
>>I thought I could do the following but it doesn't work:  


That should have work.
How do you know it's not working?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13456146
Try the following for your execl call.

execl("progb", "progb", "30", NULL);
0
 
LVL 22

Expert Comment

by:grg99
ID: 13458047
Execl expects all the parameters to be text strings, so you don't have to convert the argv[] string at all.
Just pass argv[1].

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jma2000
ID: 13458600
I need the parameter to be an int to use as a counter in called program.
0
 
LVL 22

Expert Comment

by:grg99
ID: 13458889
Every program gets an argv[] array.  It's always an array of zero-terminated strings.

If the program needs to extract an integer from it, the program does a:

int TheIntParam = atoi( argv[1] );

... it has nothing to do with the execl end of it.

0
 
LVL 22

Expert Comment

by:grg99
ID: 13458965
Oh, I see your problem.  Well, two problems.

In your called program you should really check argc before peeking at argv[1].  The reason why later.

int number;

if( argc < 2 )   number = 99999;  // whatever you want the default to be if no param specified
else
    number = atoi( argv[1] );

In general you should NEVER assume that input is always there to be grabbed.  Always check before peeking.

----

the reason this is important in this case, and would have avoided you having to call in us big guns, is that the second parameter to execl is a list of the arguments to be passed to the program INCLUDING THE [0] argument, the program name.

So you need to change "30" to "somename.exe 30".

Dont' ask me why you're allowed to execute one file and then pass it off as a different name.

.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13459102
jma2000,
Did you try my previously posted recommended fix:

execl("progb", "progb", "30", NULL);


That is the answer to yoru question.  Notice that "progb" is in there twice...

Adding validation checks is good, but will not fix the code.
The calling function needs the above change to fix the code.
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13459118
Could you please try it out, and let us know how it works out?
0
 

Author Comment

by:jma2000
ID: 13461100
Yes it worked.  Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question