Sign in Join

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

Mailing lists / fpc-pascal / 2002-08

How to Lock a file?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

mili

Hello, Every one

I opened a file with TFileStream, and I don't want any other program to access this file. So I try to lock it with FLock function in unit Linux as following:

>>>>>
var
  fs : TFileStream;
  h  : integer;
begin
  fs:=TFileStream.Create('test.dat',fmOpenReadWrite);
  h:=fs.Handle;
  if LockF(h,LOCK_EX) then writeln('Lock OK!') else writeln('Lock Failed!');
  readln;  //  ----> Now, I test if I can still open this file with other
program
  fs.Free;
end.
<<<<<<

But, Still, I can open the same file with other programs. So, Can you tell me how to lock a file correctly with free pascal?

Thank you.
mili

Reply by email

Michael Van Canneyt
to mili
mili wrote 22 lines
Hello, Every one I opened a file with TFileStream, and I don't want any other program to access this file. So I try to lock it with FLock function in unit Linux as following: >>>>>> var fs : TFileStream; h : integer; begin fs:=TFileStream.Create('test.dat',fmOpenReadWrite); h:=fs.Handle; if LockF(h,LOCK_EX) then writeln('Lock OK!') else writeln('Lock Failed!'); readln; // ----> Now, I test if I can still open this file with other program fs.Free; end. <<<<<< But, Still, I can open the same file with other programs. So, Can you tell me how to lock a file correctly with free pascal?

This is not possible on linux. Only other programs that also place a lock will fail when they try to place the lock; locking is advisory at best.

Michael.

Reply by email