From mboxrd@z Thu Jan 1 00:00:00 1970 From: "johnny_xing" Subject: RE: How to call another separate program and passing some variables in C? Date: Wed, 15 Oct 2008 17:10:36 +0800 Message-ID: References: <0E415EA45AC641298E702FBDEDADEA87@johnny> <4f5c2ba10810142334o6dbca046r52eb267c8da28aa9@mail.gmail.com> <5F844C5CE0B949348F25CDBF9B3FA11D@johnny> <4f5c2ba10810150150k153b051fod1440a14037cd16c@mail.gmail.com> <4f5c2ba10810150152s6c6307fi1fde09505b31a8bc@mail.gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: QUOTED-PRINTABLE Return-path: In-Reply-To: <4f5c2ba10810150152s6c6307fi1fde09505b31a8bc@mail.gmail.com> Sender: linux-c-programming-owner@vger.kernel.org List-ID: Content-Type: text/plain; charset="utf-8" To: 'Halesh S' Cc: korfuri@gmail.com, linux-c-programming@vger.kernel.org Hi, Halesh, Same result as above, I am wondering if system() should be used if we w= ant to pass some arguments as well. Thanks & Best Regards, =20 Johnny Xing =20 ________________________________________ =46rom: Halesh S [mailto:halesh.tux@gmail.com]=20 Sent: 2008=E5=B9=B410=E6=9C=8815=E6=97=A5 16:52 To: johnny_xing Cc: korfuri@gmail.com; linux-c-programming@vger.kernel.org Subject: Re: How to call another separate program and passing some vari= ables in C? Try program 2 as below, no need to use sprintf In program 2: int main(int argc, char *argv[]) { int input1; printf("buffer is %s\n", argv[0]); input1 =3Datoi(argv[0]); printf("input1 value is %d\n",input1); } Halesh Try program 2 as below, no need to use sprintf In program 2: int main(int argc, char *argv[]) { int input1; char buffer[10]; printf("buffer is %s\n", buffer); input1 =3Datoi(buffer); printf("input1 value is %d\n",input1); } - Halesh=20 2008/10/15 johnny_xing 2008/10/15 johnny_xing Hi, =20 I tried as what Hlalesh said and it doesn't work. =20 Here is what I have: =20 In program 1: =20 system("/usr/local/src/program2 1"); =20 In program 2: int main(int argc, char *argv[]) { int input1; char buffer[10]; sprintf(buffer, argv[0]); =20 printf("buffer is %s\n", buffer); input1 =3Datoi(buffer); printf("input1 value is %d\n",input1); } =20 The output is: =20 buffer is /usr/local/src/parallel/asterisk input1 value is 0 =20 =20 Anything wrong with it? =20 =20 Thanks & Best Regards, =20 Johnny Xing =20 ________________________________________ =46rom: Halesh S [mailto:halesh.tux@gmail.com]=20 Sent: 2008=E5=B9=B410=E6=9C=8815=E6=97=A5 14:34 To: johnny_xing Cc: korfuri@gmail.com; linux-c-programming@vger.kernel.org Subject: Re: How to call another separate program and passing some vari= ables in C? =20 =20 2008/10/15 johnny_xing 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 progr= am and it has been fixed and running that I don't know how to add in the s= econd program. However, if I could invoke the second program using system() and pass s= ome integers to the second program to generate the output I want, it will b= e 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); Use system("/usr/local/src/program2 1"); the argument will stored as string in *argv[] in 2nd prog. Convert back to integer using sprintf / any of ur own method. Halesh =20 Thanks & Best Regards, Johnny Xing -----Original Message----- =46rom: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of korfuri@gmail.com Sent: 2008=E5=B9=B410=E6=9C=8814=E6=97=A5 19:12 To: johnny_xing Cc: linux-c-programming@vger.kernel.org Subject: Re : How to call another separate program and passing some variables in C? 2008/10/14, johnny_xing : > Hi, > > Forgive me if my question is so simple as I am a newbie to programmin= g.. > > I have two C programming and I want to call second C file within my f= irst 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 majordomo@vger.kernel.org > 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 majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html -- To unsubscribe from this list: send the line "unsubscribe linux-c-progr= amming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html =20 -- To unsubscribe from this list: send the line "unsubscribe linux-c-progr= amming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html