[Overview][Constants][Types][Procedures and functions][Index] |
Duplicate one filehandle to another
Source position: bunxh.inc line 39
function FpDup2( |
fildes: cint; |
fildes2: cint |
):cint; |
var oldfile: text; |
var newfile: text |
):cint; |
var oldfile: file; |
var newfile: file |
):cint; |
Makes fildes2 or NewFile an exact copy of fildes or OldFile, after having flushed the buffer of OldFile in the case of text or untyped files.
After a call to fdup2, the 2 file descriptors point to the same physical device (a file, socket, or a terminal).
NewFile can be an assigned file. If newfile or fildes was open, it is closed first. Due to the buffering mechanism of Pascal, this has not the same functionality as the dup2 call in C. The internal Pascal buffers are not the same after this call, but when the buffers are flushed (e.g. after output), the output is sent to the same file. Doing an lseek will, however, work as in C, i.e. doing a lseek will change the fileposition in both files.
The function returns the new file descriptor number, on error -1 is returned, and the error can be retrieved with fpgeterrno
In case of error, the following error codes can be reported:
|
Duplicate a file handle |
program Example32; { Program to demonstrate the FpDup2 function. } uses BaseUnix; var f : text; i : longint; begin Assign (f,'text.txt'); Rewrite (F); For i:=1 to 10 do writeln (F,'Line : ',i); if fpdup2 (output,f)=-1 then Writeln ('Dup2 Failed !'); writeln ('This is written to stdout.'); writeln (f,'This is written to the dup file, and flushed'); flush(f); writeln; { Remove file. Comment this if you want to check flushing.} fpUnlink ('text.txt'); end.