Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[MS-DOS] Using system() and catching output

1 view
Skip to first unread message

Mike K

unread,
Jun 10, 2002, 4:20:10 PM6/10/02
to

Is it possible to use a system(x) call in a program, and catch the output of
x in the caller's stdin?
Something to the effect of:

system("type autoexec.bat");
while ( fgets(string, MAX, stdin) )
fputs(string, stderr);

OR

system("type autoexec.bat < thisprocess");

Thanks, and sorry for being off topic.

Mike

Norm Dresner

unread,
Jun 10, 2002, 7:22:19 PM6/10/02
to
Mike K <mki...@rogers.com> wrote in message
news:_R7N8.272441$t8_....@news01.bloor.is.net.cable.rogers.com...

>
> Is it possible to use a system(x) call in a program, and catch the output
of
> x in the caller's stdin?
> Something to the effect of:
>
> system("type autoexec.bat");
> while ( fgets(string, MAX, stdin) )
> fputs(string, stderr);
>
> OR
>
> system("type autoexec.bat < thisprocess");
>

AFAIK this is "easy" because the routine that initiates the child-process
(sorry for using *NIX terminology) has to provide the handles for the
child's stdXXX (in/out/err). I don't think you can actually arrange a
UNIX-style pipe where you get the output as it's being generated because
DOS-style pipes don't work that way. Even from the command-line they
require an intermediate file. You could therefore do the same thing from
your program,
system( "type autoexec.bat >tempfile" )
and then open and read from tempfile.

I have a vague recollection that it's even possible to get the output on a
line-by-line (or char-by-char) basis with some really tricky programming
but I've been doing so much Linux and Windows programming lately that I
often have to look things up that I should know just to check how to do it
in the system I'm using at the moment.

Sorry I can't be more definite.

Norm

Kenneth Brody

unread,
Jun 10, 2002, 8:34:31 PM6/10/02
to
Mike K wrote:
>
> Is it possible to use a system(x) call in a program, and catch the output of
> x in the caller's stdin?
> Something to the effect of:
>
> system("type autoexec.bat");
> while ( fgets(string, MAX, stdin) )
> fputs(string, stderr);
>
> OR
>
> system("type autoexec.bat < thisprocess");

Since MS-DOS is a single-tasking system, it cannot be done as simply
as Unix's popen() function. (That function returns a handle to either
the child process' stdin or stdout, depending on how you call it.)

The best you could do is:

system("whatever >tempfile");
result = fopen("tempfile","rt");

and then fread() from result.

Note, of course, that your program does not run in parallel with the
child process, but rather will wait until the child exits before reading
from the result file.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+


those who know me have no need of my name

unread,
Jun 11, 2002, 1:57:17 AM6/11/02
to
[fu-t set]

in comp.lang.c i read:


>Mike K <mki...@rogers.com> wrote in message
>news:_R7N8.272441$t8_....@news01.bloor.is.net.cable.rogers.com...
>>
>> Is it possible to use a system(x) call in a program, and catch the
>> output of x in the caller's stdin?

off topic for comp.lang.c (and comp.lang.c++).

> system( "type autoexec.bat >tempfile" )
>and then open and read from tempfile.
>
>I have a vague recollection that it's even possible to get the output on a
>line-by-line (or char-by-char) basis with some really tricky programming

fgets and fgetc are tricky?

--
bringing you boring signatures for 17 years

0 new messages