?
Solved

Passing int in execl

Posted on 2005-03-03
9
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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