Re: Read File Problems
Re: Read File Problems
- Subject: Re: Read File Problems
- From: Nigel Garvey <email@hidden>
- Date: Tue, 20 Mar 2001 09:37:04 +0000
Shane Stanley wrote on Tue, 20 Mar 2001 15:11:37 +1100:
>
On 20/3/01 2:27 PM +1000, Paul Berkowitz, email@hidden, wrote:
>
>
> So -- what's the point of the 'write permission' parameter, if the _only_
>
> thing you need to open for access for is to write to the file? If you can
>
> read it and get eof without opening it for access, you actually only need
>
to
>
> open it in order to write to it, so why the special parameter 'with write
>
> permission'?
>
>
Opening the file first has considerable advantages if you're not reading the
>
whole thing in at once -- it's quicker, and it automatically keeps track of
>
where you're up to.
It's also possible for a file to be open several times at once (without
write permission), each reference having its own file pointer. I imagine
this could be useful to some people.
>
I used to use a simple read for shorter files, but I started having problems
>
with scripts written that way. I _think_ it involved one or more versions of
>
system software, and whether the script had previously read/written to the
>
same file.
>
>
In any event, I decided that it's easy enough to use close/open and play it
>
safe.
My own investigation into this sort of thing last year suggests that a
"simple read" - like a read by file specification after opening for
access - first checks whether there's an open-file reference for that
file. If there is, it uses it; if not, it opens the file itself. The
reference for a freshly opened file has its file pointer set for the
beginning of the file, but a reference that has already been read or
written to will have the pointer set somewhere else. This means that if
the file is already open - either because it's in use or because some
previous error has prevented it from closing - a simple read has a good
chance of not getting the expected results. You can get round this using
the 'from' and 'to' parameters, but then you screw up the file pointer
for whatever process opened the file in the first place - which may not
be the current script.
NG