Mail Archives: djgpp/1999/08/05/16:47:27
On Wed, 4 Aug 1999, Edevaldo Pereira da Silva Junior wrote:
> One more question. Is there a way, in DJGPP, to write to the standard
> input of a program, run it and the read it's standard output?
> I'm asking that because reading popen and pclose documentation I got
> the impression that I can only write to the standard input or read the
> standard output. Not both. Am I right?
Yes, you are right. If you need two-way communications to the
subprogram, you will have to add some code. For example (UNTESTED):
FILE *fptem, *fpipe;
char tem_name[L_tmpnam], cmd_line[L_tmpnam + FILENAME_MAX + 4];
fptem = fopen (tmpnam (tem_name), "w");
fprintf (fptem, ....); /* write program's input to temp file */
fclose (fptem);
sprintf (cmd_line, "prog.exe < %s", tem_name)
fpipe = popen (cmd_line, "r"); /* run the program */
remove (tem_name); /* delete temporary file */
fread (fpipe, ... ); /* read program's output */
pclose (fpipe); /* close the pipe */
Note that I left out a lot of error-checking. In particular, popen
and pclose can return error indications that you shouldn't ignore.
Btw, I suggest to use "prog.exe" in the command line and not just
"prog" because that makes the error checking smarter: in the former
case you will get NULL from popen and errno will be set to ENOENT if
the program isn't found, while in the latter case you will only see
"Bad command or file name", since popen will eventually call
COMMAND.COM.
- Raw text -