* How to call another separate program and passing some variables in C? @ 2008-10-14 10:48 johnny_xing 2008-10-14 11:12 ` Re : " korfuri 0 siblings, 1 reply; 8+ messages in thread From: johnny_xing @ 2008-10-14 10:48 UTC (permalink / raw) To: linux-c-programming 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 majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re : How to call another separate program and passing some variables in C? 2008-10-14 10:48 How to call another separate program and passing some variables in C? johnny_xing @ 2008-10-14 11:12 ` korfuri 2008-10-15 6:27 ` johnny_xing 0 siblings, 1 reply; 8+ messages in thread From: korfuri @ 2008-10-14 11:12 UTC (permalink / raw) To: johnny_xing; +Cc: linux-c-programming 2008/10/14, johnny_xing <johnny_xing@banshing.com>: > 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 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: How to call another separate program and passing some variables in C? 2008-10-14 11:12 ` Re : " korfuri @ 2008-10-15 6:27 ` johnny_xing [not found] ` <4f5c2ba10810142334o6dbca046r52eb267c8da28aa9@mail.gmail.com> 0 siblings, 1 reply; 8+ messages in thread From: johnny_xing @ 2008-10-15 6:27 UTC (permalink / raw) To: korfuri; +Cc: linux-c-programming 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: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of korfuri@gmail.com Sent: 2008年10月14日 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 <johnny_xing@banshing.com>: > 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 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <4f5c2ba10810142334o6dbca046r52eb267c8da28aa9@mail.gmail.com>]
* RE: How to call another separate program and passing some variables in C? [not found] ` <4f5c2ba10810142334o6dbca046r52eb267c8da28aa9@mail.gmail.com> @ 2008-10-15 8:31 ` johnny_xing 2008-10-15 9:05 ` Per Jessen [not found] ` <5F844C5CE0B949348F25CDBF9B3FA11D@johnny> 1 sibling, 1 reply; 8+ messages in thread From: johnny_xing @ 2008-10-15 8:31 UTC (permalink / raw) To: 'Halesh S'; +Cc: korfuri, linux-c-programming Hi, I tried as what Hlalesh said and it doesn’t work. Here is what I have: In program 1: system(“/usr/local/src/program2 1”); In program 2: int main(int argc, char *argv[]) { int input1; char buffer[10]; sprintf(buffer, argv[0]); printf("buffer is %s\n", buffer); input1 =atoi(buffer); printf("input1 value is %d\n",input1); } The output is: buffer is /usr/local/src/parallel/asterisk input1 value is 0 Anything wrong with it? Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 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 variables in C? 2008/10/15 johnny_xing <johnny_xing@banshing.com> 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); 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 Thanks & Best Regards, Johnny Xing -----Original Message----- From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of korfuri@gmail.com Sent: 2008年10月14日 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 <johnny_xing@banshing.com>: > 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 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-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-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to call another separate program and passing some variables in C? 2008-10-15 8:31 ` johnny_xing @ 2008-10-15 9:05 ` Per Jessen 0 siblings, 0 replies; 8+ messages in thread From: Per Jessen @ 2008-10-15 9:05 UTC (permalink / raw) To: C programming list johnny_xing wrote: > Hi, > > I tried as what Hlalesh said and it doesn’t work. > > Here is what I have: > > In program 1: > > system(“/usr/local/src/program2 1”); > > In program 2: > int main(int argc, char *argv[]) > { > int input1; > char buffer[10]; > sprintf(buffer, argv[0]); > > printf("buffer is %s\n", buffer); > input1 =atoi(buffer); > printf("input1 value is %d\n",input1); > } > > The output is: > > buffer is /usr/local/src/parallel/asterisk > input1 value is 0 > argv[0] is always the name of the called binary. To get the arguments, you need to start with argv[1]. Surely you can see that atoi("/usr/local/src/parallel/asterisk") is 0 as there is no way to convert "/usr/local/src/parallel/asterisk" to an integer. /Per Jessen, Zurich -- 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <5F844C5CE0B949348F25CDBF9B3FA11D@johnny>]
[parent not found: <4f5c2ba10810150150k153b051fod1440a14037cd16c@mail.gmail.com>]
[parent not found: <4f5c2ba10810150152s6c6307fi1fde09505b31a8bc@mail.gmail.com>]
* RE: How to call another separate program and passing some variables in C? [not found] ` <4f5c2ba10810150152s6c6307fi1fde09505b31a8bc@mail.gmail.com> @ 2008-10-15 9:10 ` johnny_xing [not found] ` <4f5c2ba10810150227k44321974n4fe51ff28e470e2c@mail.gmail.com> 2008-10-16 0:41 ` Jianjun Kong 0 siblings, 2 replies; 8+ messages in thread From: johnny_xing @ 2008-10-15 9:10 UTC (permalink / raw) To: 'Halesh S'; +Cc: korfuri, linux-c-programming Hi, Halesh, Same result as above, I am wondering if system() should be used if we want to pass some arguments as well. Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 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 variables 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 =atoi(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 =atoi(buffer); printf("input1 value is %d\n",input1); } - Halesh 2008/10/15 johnny_xing <johnny_xing@banshing.com> 2008/10/15 johnny_xing <johnny_xing@banshing.com> Hi, I tried as what Hlalesh said and it doesn't work. Here is what I have: In program 1: system("/usr/local/src/program2 1"); In program 2: int main(int argc, char *argv[]) { int input1; char buffer[10]; sprintf(buffer, argv[0]); printf("buffer is %s\n", buffer); input1 =atoi(buffer); printf("input1 value is %d\n",input1); } The output is: buffer is /usr/local/src/parallel/asterisk input1 value is 0 Anything wrong with it? Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 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 variables in C? 2008/10/15 johnny_xing <johnny_xing@banshing.com> 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); 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 Thanks & Best Regards, Johnny Xing -----Original Message----- From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of korfuri@gmail.com Sent: 2008年10月14日 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 <johnny_xing@banshing.com>: > 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 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-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-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <4f5c2ba10810150227k44321974n4fe51ff28e470e2c@mail.gmail.com>]
* RE: How to call another separate program and passing some variables in C? [not found] ` <4f5c2ba10810150227k44321974n4fe51ff28e470e2c@mail.gmail.com> @ 2008-10-15 9:35 ` johnny_xing 0 siblings, 0 replies; 8+ messages in thread From: johnny_xing @ 2008-10-15 9:35 UTC (permalink / raw) To: 'Halesh S'; +Cc: korfuri, linux-c-programming Yes that works for me as well. Thanks a lot guys. Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 17:28 To: johnny_xing Cc: korfuri@gmail.com; linux-c-programming@vger.kernel.org Subject: Re: How to call another separate program and passing some variables in C? Please check below argv[0] will have prgram name itself...so its giving 0... Start from argv[1] :) Try below, I am able to valid result. prog1.c ------------ int main() { system("./prog2 1 2 3"); exit(0); } prog2.c ------------- int main(int argc, char *argv[]) { int input1,i; for (i=1; i<argc; i++) { - Hide quoted text - printf("buffer is %s\n", argv[i]); input1 =atoi(argv[i]); printf("input1 value is %d\n",input1); } } run prog1, i am able to get proper result. Thanks, Halesh On Wed, Oct 15, 2008 at 2:40 PM, johnny_xing <johnny_xing@banshing.com> wrote: Hi, Halesh, Same result as above, I am wondering if system() should be used if we want to pass some arguments as well. Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 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 variables 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 =atoi(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 =atoi(buffer); printf("input1 value is %d\n",input1); } - Halesh 2008/10/15 johnny_xing <johnny_xing@banshing.com> 2008/10/15 johnny_xing <johnny_xing@banshing.com> Hi, I tried as what Hlalesh said and it doesn't work. Here is what I have: In program 1: system("/usr/local/src/program2 1"); In program 2: int main(int argc, char *argv[]) { int input1; char buffer[10]; sprintf(buffer, argv[0]); printf("buffer is %s\n", buffer); input1 =atoi(buffer); printf("input1 value is %d\n",input1); } The output is: buffer is /usr/local/src/parallel/asterisk input1 value is 0 Anything wrong with it? Thanks & Best Regards, Johnny Xing ________________________________________ From: Halesh S [mailto:halesh.tux@gmail.com] Sent: 2008年10月15日 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 variables in C? 2008/10/15 johnny_xing <johnny_xing@banshing.com> 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); 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 Thanks & Best Regards, Johnny Xing -----Original Message----- From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of korfuri@gmail.com Sent: 2008年10月14日 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 <johnny_xing@banshing.com>: > 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 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-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-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to call another separate program and passing some variables in C? 2008-10-15 9:10 ` johnny_xing [not found] ` <4f5c2ba10810150227k44321974n4fe51ff28e470e2c@mail.gmail.com> @ 2008-10-16 0:41 ` Jianjun Kong 1 sibling, 0 replies; 8+ messages in thread From: Jianjun Kong @ 2008-10-16 0:41 UTC (permalink / raw) To: johnny_xing; +Cc: 'Halesh S', korfuri, linux-c-programming On Wed, Oct 15, 2008 at 05:10:36PM +0800, johnny_xing wrote: >Hi, Halesh, > >Same result as above, I am wondering if system() should be used if we want to pass some arguments as well. $ man system You can get more information :) >Thanks & Best Regards, > >Johnny Xing ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2008-10-16 0:41 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-14 10:48 How to call another separate program and passing some variables in C? johnny_xing
2008-10-14 11:12 ` Re : " korfuri
2008-10-15 6:27 ` johnny_xing
[not found] ` <4f5c2ba10810142334o6dbca046r52eb267c8da28aa9@mail.gmail.com>
2008-10-15 8:31 ` johnny_xing
2008-10-15 9:05 ` Per Jessen
[not found] ` <5F844C5CE0B949348F25CDBF9B3FA11D@johnny>
[not found] ` <4f5c2ba10810150150k153b051fod1440a14037cd16c@mail.gmail.com>
[not found] ` <4f5c2ba10810150152s6c6307fi1fde09505b31a8bc@mail.gmail.com>
2008-10-15 9:10 ` johnny_xing
[not found] ` <4f5c2ba10810150227k44321974n4fe51ff28e470e2c@mail.gmail.com>
2008-10-15 9:35 ` johnny_xing
2008-10-16 0:41 ` Jianjun Kong
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).