[Overview][Constants][Types][Procedures and functions][Variables][Index] |
Assign stream for in and output to a program
Source position: line 0
function AssignStream( |
var StreamIn: text; |
var Streamout: text; |
const Prog: String |
):LongInt; |
var StreamIn: Text; |
var StreamOut: Text; |
var StreamErr: Text; |
const prog: String |
):LongInt; |
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 name of a program (including path) with options, which will be executed.
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.
In case of error (return value -1) LinuxError is used to report errors:
Other errors include the ones by the fork and exec programs
|
Create a set of pipe file handlers |
|
|
Pipe file to standard input/output of program |
Program Example38; { Program to demonstrate the AssignStream function. } Uses oldlinux; Var Si,So : Text; S : String; i : longint; begin if not (paramstr(1)='-son') then begin Writeln ('Calling son'); Assignstream (Si,So,'./ex38 -son'); if linuxerror<>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.
lazarus-ccr.sourceforge.net |