Operating System - HP-UX
1830624 Members
2421 Online
110015 Solutions
New Discussion

sending int variables from one c to another c program

 
SOLVED
Go to solution
Xiaoming Zhang
Contributor

sending int variables from one c to another c program

I ran following c program:
#include
#include
#include
main()
{
int num1, num2;
num1=2; num2=115;
if ( fork() == 0 )
{
cout << " fork() call was sucessful. \n";
cout << num1 << " " << num2 << endl;
if (execlp("test2.exe",(char*)0)<0) cout << "execlp call faile
d\n" ;
}
sleep(2);
}

which invokes test2.exe:
#include

void main(int argc, char *argv[])
{
int passin1, passin2;
if ( argc == 2)
{
passin1=(int)*argv[1]; passin2=(int)*argv[2];
}
else
{
passin1=4; passin2=238;
}
cout << "entering test2.exe" << endl;
cout << passin1 << " " << passin2 << endl;
}

My problem is how can I pass num1 and num2's values from invoking program to invoked program?

Thanks.

Xiaoming
1 REPLY 1
Thomas Kollig
Trusted Contributor
Solution

Re: sending int variables from one c to another c program

It works like this:

test1.C:
...
char n1[100];
char n2[100];
...
sprintf(n1,"%d",num1);
sprintf(n2,"%d",num2);
...
...execlp("test2.exe","test2.exe",n1,n2,(char*)0)...
/* arg0 == file (man-pages) */
...

in test2.C:
...
if (argc == 3)
/* first is the program name */
...
passin1=atoi(argv[1]);
passin2=atoi(argv[2]);
...

Bye,

Thomas