FpDup2 dosen't work!

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

FpDup2 dosen't work!

Linuxer Wang
Hi, all

I can't make FpDup2 work. Even the example provided by the document
can't work as well.
My system is fedora4 linux, freepascal 2.0.0 [2005/08/17] for i386.
Can anybody help me?
Thanks

program Example31;

{ Program to demonstrate the Dup 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)<>0 then
Writeln ('Dup2 Failed !'); //------------------------------ here, fpdup2
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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: FpDup2 dosen't work!

Jonas Maebe-2

On 19 okt 2005, at 03:16, Linuxer Wang wrote:

> I can't make FpDup2 work. Even the example provided by the document
> can't work as well.

It seems to me the fpdup2 simply does something different than what  
the example program suggests. It creates a copy of the first file  
record into the second. The underlying dup2 system call or libc  
function first closes the second file if it was open.

So after the fpdup2() call, all writes to the second file you passed  
it will go to the same place as writes to the first one (and this  
indeed works in the example program: all writers to f after the fpdup2
() call also appear on screen). It does not allow you to "clone" the  
output to a different place.

The error checking in the example program is also wrong. fpdup2  
returns -1 on error. If no error occurred, it returns the file handle  
corresponding to file of the second parameter (which usually will be  
different from 0, although not always -- namely when you dup  
something to standard input).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal