Unix Programming - execute a command and return the result

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > execute a command and return the result





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author execute a command and return the result
puzzlecracker

2005-11-01, 2:59 am

basically i want to something like that
int
main()
{

char * str;
str=system ("cmd arg1 arg3");

printf("%s\n",str);


}

How is it generally done?

Thanks

Barry Margolin

2005-11-01, 2:59 am

In article <1130819764.080902.146630@z14g2000cwz.googlegroups.com>,
"puzzlecracker" <ironsel2000@gmail.com> wrote:

> basically i want to something like that
> int
> main()
> {
>
> char * str;
> str=system ("cmd arg1 arg3");
>
> printf("%s\n",str);
>
>
> }
>
> How is it generally done?


Use popen().

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Prafulla Harpanhalli

2005-11-01, 2:59 am


puzzlecracker wrote:
> basically i want to something like that
> int
> main()
> {
>
> char * str;
> str=system ("cmd arg1 arg3");
>
> printf("%s\n",str);
>
>
> }
>
> How is it generally done?
>
> Thanks


Your usage of system() seems OK provided the args that you pass are
well defined, which is not the case in your listing.

Also, return type of system() is int. Your compiler should have thrown
a diagnostic for this program. Did you try compiling it?

--
Prafulla Harpanhalli

John Gordon

2005-11-01, 6:11 pm

In <1130819764.080902.146630@z14g2000cwz.googlegroups.com> "puzzlecracker" <ironsel2000@gmail.com> writes:

> basically i want to something like that
> int
> main()
> {


> char * str;
> str=system ("cmd arg1 arg3");


> printf("%s\n",str);



> }


> How is it generally done?


with popen().

FILE *fp;
char buffer[99];

fp = popen("cmd arg1 arg2", "r");
fgets(buffer, sizeof(buffer), fp);
pclose(fp);

printf("%s\n", buffer);

--
John Gordon "It's certainly uncontaminated by cheese."
gordon@panix.com

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com