Board index » delphi » Is a text file open or closed ???

Is a text file open or closed ???

How can I check if a text file is open or closed in D1 & D3 ??

I have tried:

if TTextRec(F).Mode = fmClosed then .....

but I have to initializes the "TTextRec(F).Mode" to "fmClosed" and that is a
problem for me....

Thanks in advance

--
Klaus Kristensen
Denmark in Europe

 

Re:Is a text file open or closed ???


Quote
Duncan Murdoch <dmurd...@pair.com> wrote:
> If a variable isn't initialized, then it could contain anything at
> all, so there's no way to avoid some sort of initialization of F if
> you want to test it.

> Assignfile will do the initialization for you, you don't need to
> fiddle with the Mode field.

I use "AssignFile" and "Reset" in the opening process, but how do I avoid
other users of the software to assign and reset the same text file, if the
text file have not been closed yet. How do I test if a text file is open or
closed?

Thanks in advance.

Klaus Kristensen
Denmark in Europe

Re:Is a text file open or closed ???


Quote
On Mon, 03 Apr 2000 07:53:07 GMT, "Klaus Kristensen" <x...@x.dk> wrote:
>I use "AssignFile" and "Reset" in the opening process, but how do I avoid
>other users of the software to assign and reset the same text file, if the
>text file have not been closed yet. How do I test if a text file is open or
>closed?

You mean to avoid sharing violations?  I think the only way to do that
is to attempt to open it; if it fails, there was a problem, and the
error code will indicate something about what the problem was.  It has
to be this way, otherwise you'd have situations like this:

1.  You test the file and it's closed.
2.  Someone else opens it.
3.  You try to open it and your program crashes, because you assumed
it would still be closed.

By the way, you can play around with the "FileMode" variable to change
the sharing access mode when opening binary files, but you're stuck
with the standard modes with text files.

Duncan Murdoch

Re:Is a text file open or closed ???


Hi.

Quote
> I use "AssignFile" and "Reset" in the opening process, but how do I avoid
> other users of the software to assign and reset the same text file, if the
> text file have not been closed yet. How do I test if a text file is open
or
> closed?

Try locking and unlocking file. See win32.hlp for more info.

Bye.
/lexi

Re:Is a text file open or closed ???


Nope..
He should use SHFileOperation winApi function. Look winapi help. The best
way you can do the job.

---
FA Beli/Fleet Commader/PLT Loch/ISF
[MoIx4][IDMx2][SSx2][GS][MoH]
ISF-1998030106
http://www.imperialstarfleet.com
---
Jedi Master Beli/ATD/Dark Council/Advisor of SoH
[VC][CD][BTM][STM][GTM][GSC]
http://www.sauron.co.yu/atd
---
http://www.sauron.co.yu
ICQ No. 1878554

Quote
----- Original Message -----
From: "Duncan Murdoch" <dmurd...@pair.com>
To: <beli-n...@fido.org.yu>
Sent: Monday, 03 April, 2000 9:43 AM
Subject: Re: Is a text file open or closed ???

> On Mon, 03 Apr 2000 07:53:07 GMT, "Klaus Kristensen" <x...@x.dk> wrote:

> >I use "AssignFile" and "Reset" in the opening process, but how do I avoid
> >other users of the software to assign and reset the same text file, if
the
> >text file have not been closed yet. How do I test if a text file is open
or
> >closed?

> You mean to avoid sharing violations?  I think the only way to do that
> is to attempt to open it; if it fails, there was a problem, and the
> error code will indicate something about what the problem was.  It has
> to be this way, otherwise you'd have situations like this:

> 1.  You test the file and it's closed.
> 2.  Someone else opens it.
> 3.  You try to open it and your program crashes, because you assumed
> it would still be closed.

> By the way, you can play around with the "FileMode" variable to change
> the sharing access mode when opening binary files, but you're stuck
> with the standard modes with text files.

> Duncan Murdoch

Re:Is a text file open or closed ???


On 4 Apr 2000 04:30:56 +0200, f...@imperialstarfleet.com (Beli) wrote:

Quote
>Nope..
>He should use SHFileOperation winApi function. Look winapi help. The best
>way you can do the job.

I think you picked the wrong function.  That's a shell function that
"Performs a copy, move, rename, or delete operation on a file system
object."  Klaus wanted to determine whether a file was already open.

Duncan Murdoch

Re:Is a text file open or closed ???


Quote
> By the way, you can play around with the "FileMode" variable to change
> the sharing access mode when opening binary files, but you're stuck
> with the standard modes with text files.

Can I use "FileMode" = (0  or 1  or 2) for text files ??

Klaus Kristensen
Denmark in Europe

Re:Is a text file open or closed ???


Quote
On Wed, 05 Apr 2000 07:28:19 GMT, "Klaus Kristensen" <x...@x.dk> wrote:
>> By the way, you can play around with the "FileMode" variable to change
>> the sharing access mode when opening binary files, but you're stuck
>> with the standard modes with text files.

>Can I use "FileMode" = (0  or 1  or 2) for text files ??

Those are the modes used by the default text file device drivers, but
(last time I looked) they're hard coded values:  the FileMode variable
isn't used. It's only used with Reset on a binary file.

You can change the file mode for text files, but it means writing your
own Assign function and text file device drivers to go with it.  

Duncan Murdoch

Re:Is a text file open or closed ???


Try this:

Procedure IsOpen;
var
   OpenFile : boolean;
   Param    : file;
   OpenFile : string; { FileName that is assigned }
begin
     assignfile(Param,OpenFile);
     {$I-}
     reset(Param); { if error try resetfile() }
     {$I+}
     if IOResult <> 0 then
     begin
          { File not open }
          OpenFile := false;
          try
             close(param);
          except
          end;
     end
     else
     begin
          { File is open }
          OpenFile := true;
          try
             close(param);
          except
          end;
     end;
end;

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Is a text file open or closed ???


There is a fault in my example > duplicate of var name
rename OpenFile : boolean to something like IsOpenFile

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Other Threads