Re: Reading and Writing to text files
Re: Reading and Writing to text files
- Subject: Re: Reading and Writing to text files
- From: Nigel Garvey <email@hidden>
- Date: Fri, 5 Jan 2001 02:56:57 +0000
Victor Yee wrote on Thu, 4 Jan 2001 12:33:20 -0500:
>
You may be confusing a file specification with a file access ID.
>
>
When you issue an open for access command, the result is a file access ID
>
number (e.g. --> 608).
>
>
The easiest way to use the access ID would be to assign the result of the
>
request to a variable:
>
>
set thisFileAccessID to open for access file thisLogFilePath
>
read thisFileAccessID
>
close access thisFileAccessID
>
>
The confusion may be arising from your use of the read command, where you
>
direct the script to read a file using a file specification:
>
>
read file thisLogFilePath
Strictly speaking, thisLogFilePath was a string in the original script,
which was no doubt being invisibly coerced to a file specification by
Jon's Commands.
>
which works fine for one-shot reading, but may create the illusion that
>
you're using the same access that you opened earlier. What is actually
>
happening is that a new access to the file is created, the file read
>
through this access, then closed automatically.
>
>
Writing is only allowed through file access IDs, not through file
>
specifications.
Victor, I think your advice to use the file access ID's is spot on, but
my own experiments while trying to track down some sporadic end-of-file
errors suggest the following:
1) A file may be opened any number of times at once for access (maybe
there's a theoretical upper limit, I don't know), each opening returning
a file access ID. When the file is opened, the file pointer associated
with that particular access ID is set to 1.
2) Only one access to a file can have write permission. Until this is
closed, the file can only be *read* by other accesses.
3) When you read from, or write to, a file using a file specification,
the command looks to see if there's already an access open for it. If
there is, then the command adopts the first access ID it finds for that
file. If this ID happens to be one that has write permission, a write
command is successful. If not, or if the file isn't open, a write command
will fail with the "write permission" error. In the case of a read
command, if the access ID adopted has been used already, the command will
fail with an end-of-file error if the file pointer's at the end of the
file or will be incomplete unless a 'from 1' parameter is used.
If the file *isn't* already open, then a read command with file
specification will open it for itself and close it again afterwards.
And you thought your explanation was complicated! :-)
NG