Re: Open files
Re: Open files
- Subject: Re: Open files
- From: Nigel Garvey <email@hidden>
- Date: Mon, 18 Mar 2002 14:32:47 +0000
Ed Stockly wrote on Sun, 17 Mar 2002 14:59:04 -0800:
>
> it's possible for a file to be open several times at once, each opening
>
> having its own reference and associated file pointer. Only one of these
>
> references can have write permission. If that's the reference you've
>
> lost, you'll have to keep closing the file by path until the
>
> appropriate reference surfaces and its closure enables you to reopen
>
> the file with write permission. This may have implications for other
>
> processes which haven't lost their references and are trying to read
>
> the file.
>
>
>
> NG
>
>
Wow. You learn something new every day. I didn't think there were any
>
cases where using the file reference number was more reliable, but it
>
seems there is one.
>
>
I'm not convinced this is enough of a potential problem to change the
>
way I use the open/read/write/close commands, or to tell people that
>
using the file ref number is required, but it certainly is good to know
>
in case that error ever comes up.
Well yes, using the file ref number isn't absolutely "required", but
scripters ought to be aware of the issues in order to be able to make an
informed decision about what they use and how they minimise the risks
associated with their choice.
For instance, after a file's opened for access, further operations on it
should be slightly faster using the reference rather than the path,
simply because it saves the system having to look for the reference that
matches the path each time. This is something to consider if you're going
to perform several actions on the file before closing it. It also ensures
that the script uses its own reference to the file and doesn't mess up
some other process's access to it - and doesn't have its own access
messed up either.
On the other hand, for a one-off read, it might not be worth using 'open
for access' at all. Simply using the 'read' command with the file path
will open the file, perform the read as specified, and close the file
again afterwards - unless, of course, the file's already open, in which
case the read will hijack the existing reference, with all that that
implies for your script and whatever opened the file.
The reason I looked into all this in the first place was because I and
others were getting occasional end-of-file errors when using 'read' this
way. It turned out to be due to files still being open from earlier
script crashes, having previously been read (or written to) right through
to the end. The file pointers (or "file marks") associated with the
existing references were thus pointing to the wrong end of the files and
trying to read beyond there was what was causing the errors.
There are ways to script round the possible problems. My point is that
there's more involved than just alternative syntax.
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.