|
Home > Archive > Unix Programming > January 2005 > Redirecting stdout in program?
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 |
Redirecting stdout in program?
|
|
| Ron Olson 2005-01-13, 5:53 pm |
| Hi all-
I am trying to write a program in C that executes a separate child
process (fortune, say) and gets the output from stdout to a buffer in
the parent process so that I can manipulate it. A hacked up version of
what I've been working with is:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
printf("ready\n");
fflush(stdout);
int commpipe[2];
pipe(commpipe);
dup2(commpipe[1], 1); /* 0 = stdin, 1 = stdout */
int ret = execl("/usr/local/bin/fortune", "/usr/local/bin/fortune",
(char*)NULL);
setbuf(stdout, NULL);
printf("okay\n");
char szData[256 + 1] = {0};
while (fgets(szData, sizeof(szData), stdin))
{
printf("--> %s\n", szData);
}
printf("ret is %d\n", ret);
return 0;
}
Problem is, this doesn't return anything, not even the "okay" nor the
"ret is..".
Might anyone have any pointers (no pun intended) on getting pipe() and
dup2() to work in this context. If my approach is completely incorrect,
I'd love suggestions on how to make it work.
Thanks for any info,
Ron
| |
| Paul Pluzhnikov 2005-01-13, 5:53 pm |
| Ron Olson <olsonr@panix.com> writes:
> I am trying to write a program in C that executes a separate child
> process (fortune, say) and gets the output from stdout to a buffer in
> the parent process so that I can manipulate it.
man popen
> Problem is, this doesn't return anything, not even the "okay" nor the
> "ret is..".
Naturally. Try "man execl" and "man fork".
After the call to execl(), your program no longer exists.
You must fork() a child first.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Chuck Dillon 2005-01-13, 5:53 pm |
| Ron Olson wrote:
> Hi all-
>
> I am trying to write a program in C that executes a separate child
> process (fortune, say) and gets the output from stdout to a buffer in
> the parent process so that I can manipulate it. A hacked up version of
> what I've been working with is:
>
> #include <stdio.h>
> #include <unistd.h>
> int main(int argc, char* argv[])
> {
> printf("ready\n");
> fflush(stdout);
> int commpipe[2];
> pipe(commpipe);
> dup2(commpipe[1], 1); /* 0 = stdin, 1 = stdout */
>
> int ret = execl("/usr/local/bin/fortune", "/usr/local/bin/fortune",
You just replaced your program with fortune so nothing beyond this
point gets executed. See
http://www.faqs.org/faqs/unix-faq/p.../faq/index.html and look
for fork (item 1 I believe), exec and pipe.
After you get it working look up popen.
-- ced
> (char*)NULL);
> setbuf(stdout, NULL);
> printf("okay\n");
> char szData[256 + 1] = {0};
> while (fgets(szData, sizeof(szData), stdin))
> {
> printf("--> %s\n", szData);
> }
> printf("ret is %d\n", ret);
>
> return 0;
> }
>
> Problem is, this doesn't return anything, not even the "okay" nor the
> "ret is..".
>
> Might anyone have any pointers (no pun intended) on getting pipe() and
> dup2() to work in this context. If my approach is completely incorrect,
> I'd love suggestions on how to make it work.
>
> Thanks for any info,
>
> Ron
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
| |
| David Schwartz 2005-01-13, 5:53 pm |
|
"Ron Olson" <olsonr@panix.com> wrote in message
news:olsonr-982E36.09375313012005@reader2.panix.com...
> Hi all-
>
> I am trying to write a program in C that executes a separate child
> process (fortune, say) and gets the output from stdout to a buffer in
> the parent process so that I can manipulate it. A hacked up version of
> what I've been working with is:
>
> #include <stdio.h>
> #include <unistd.h>
> int main(int argc, char* argv[])
> {
> printf("ready\n");
> fflush(stdout);
> int commpipe[2];
> pipe(commpipe);
> dup2(commpipe[1], 1); /* 0 = stdin, 1 = stdout */
>
> int ret = execl("/usr/local/bin/fortune", "/usr/local/bin/fortune",
> (char*)NULL);
At this point, your program ceases to exist, being replaced with a copy
of fortune. What you wanted to do was call 'fork'. In the child, you close
the read side of the pipe and dup the write side on stdout, then exec
fortune. In the parent, you close the write side of the pipe, and read from
the read side.
DS
|
|
|
|
|