Unit 'Unix' Package
[Overview][Constants][Types][Procedures and functions][Index] [#rtl]

AssignStream

Assign stream for in and output to a program.

Declaration

Source position: unix.pp line 157

function AssignStream(

  var StreamIn: text;

  var Streamout: text;

  const Prog: ansiString;

  const args: array of ansistring

):cint;

function AssignStream(

  var StreamIn: text;

  var Streamout: text;

  var streamerr: text;

  const Prog: ansiString;

  const args: array of ansistring

):cint;

Description

AssignStream creates a 2 or 3 pipes, i.e. two (or three) file objects, one for input, one for output,(and one for standard error) the other ends of these pipes are connected to standard input and output (and standard error) of Prog. Prog is the path of a program (including path). The options for the program can be specified in Args.

What is written to StreamOut, will go to the standard input of Prog. Whatever is written by Prog to it's standard output can be read from StreamIn. Whatever is written by Prog to it's standard error read from StreamErr, if present.

Reading and writing happens through the usual Readln(StreamIn,...) and Writeln (StreamOut,...) procedures.

Remark: You should not use Reset or Rewrite on a file opened with POpen. This will close the file before re-opening it again, thereby closing the connection with the program.

The function returns the process ID of the spawned process, or -1 in case of error.

Errors

Extended error information is returned by the FpGetErrno function.

sys_emfile
Too many file descriptors for this process.
sys_enfile
The system file table is full.

Other errors include the ones by the fork and exec programs

See also

AssignPipe

  

Create a set of pipe file handlers.

POpen

  

Pipe file to standard input/output of program.

Example

Program Example38;

{ Program to demonstrate the AssignStream function. }

Uses BaseUnix,Unix;

Var Si,So : Text;
    S : String;
    i : longint;

begin
  if not (paramstr(1)='-son') then
    begin
    Writeln ('Calling son');
    Assignstream (Si,So,paramstr(0),['-son']);
    if fpgeterrno<>0 then
      begin
      writeln ('AssignStream failed !');
      halt(1);
      end;
    Writeln ('Speaking to son');
    For i:=1 to 10 do
      begin
      writeln (so,'Hello son !');
      if ioresult<>0 then writeln ('Can''t speak to son...');
      end;
    For i:=1 to 3 do writeln (so,'Hello chap !');
    close (so);
    while not eof(si) do
      begin
      readln (si,s);
      writeln ('Father: Son said : ',S);
      end;
    Writeln ('Stopped conversation');
    Close (Si);
    Writeln ('Put down phone');
    end
  Else
    begin
    Writeln ('This is the son ');
    While not eof (input) do
      begin
      readln (s);
      if pos ('Hello son !',S)<>0 then
         Writeln ('Hello Dad !')
      else
         writeln ('Who are you ?');
      end;
    close (output);
    end
end.

Documentation generated on: Nov 22 2024