RE: How to call another separate program and passing some variables in C?

"johnny_xing" <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <0E415EA45AC641298E702FBDEDADEA87@johnny>
Hi, Uriel

Yes I need to execute two separate programs although I know it would be much
easier if I could put into one program.

The reason is the first program invokes lots of other modules and program
and it has been fixed and running that I don't know how to add in the second
program.

However, if I could invoke the second program using system() and pass some
integers to the second program to generate the output I want, it will be
good enough.

Eg: system("/usr/local/src/program2"); 

The problem is I don't know how to pass some integers upon invoking it.

Is it something like this if I want to pass an integer 1?
system("/usr/local/src/program2" 1);

Thanks & Best Regards,
 
Johnny Xing
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
[email protected]
Sent: 20081014 19:12
To: johnny_xing
Cc: [email protected]
Subject: Re : How to call another separate program and passing some
variables in C?

2008/10/14, johnny_xing <[email protected]>:
> Hi,
>
> Forgive me if my question is so simple as I am a newbie to programming..
>
> I have two C programming and I want to call second C file within my first
C
> program, upon calling, I need to pass some variables (like 1 or 0) to the
> second program.
>
> Can anyone tell me how to do? I searched system() but it seems not be able
> to do this.
>
> Thanks & Best Regards,
>
> Johnny
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to [email protected]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

Hi,

You can use system() if you build the command-line (e.g. with
sprintf()) to pass the arguments you want. Then you can access it in
your 2nd program with argc/argv.

An alternative is to use fork() and execve(). Please note that you
have to convert each argument to a string so execve() can use it. Here
again you can use sprintf() for this purpose.

But are you sure you need two separate programs ? You can build 2 C
files together into one binary. That makes passing variables much
easier as it results to a normal function call.

Hope this will help
Regards,
Uriel

-- 
Uriel "Korfuri" Corfa
Epitech student
--
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.