www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/05/16:47:27

Date: Thu, 5 Aug 1999 10:08:32 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Edevaldo Pereira da Silva Junior <Edevaldo DOT Pereira AT motorola DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: TCL Port to DJGPP - Bug + Advices.
In-Reply-To: <37A88DD8.4A4AD959@motorola.com>
Message-ID: <Pine.SUN.3.91.990805100812.14908F-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019