Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-10

FpDup2 dosen't work!

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Reply by email

Jonas Maebe
to Linuxer Wang
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

Reply by email