[Coco] RS-DOS: EOF() but for writing?
William Astle
lost at l-w.ca
Thu Jan 26 16:00:43 EST 2023
Writing past the end of the file will just make the file bigger unless
you end up with a "Disk Full" error. Same thing for a PUT past the end
of the file - it just extends the file if it can. So you have to get clever.
There isn't any handy "tell()" function that gives the current file
position for sequential files. There also isn't a SEEK command either.
So that makes things less easy than it would otherwise be.
You can use FREE() to identify if the next write will definitely
succeed. If FREE() returns a number greater than 0, there's still a free
granule that can be allocated to extend the file. Presumably this will
be the case the vast majority of the time and it should guarantee that
the next write will succeed for either sequential or random files.
If FREE() returns 0, then you have to be more clever.
For sequential files, that means you have to keep count of every byte
you write to the file. However, since "O" mode files are truncated, you
don't have to figure out how big the file was when you opened it. But
you do need to convert every write to a string so you can identify how
long it is, and include any terminator/delimiters that will be written
by Disk Basic as well. You have to do this for *every* write whether
regardless what FREE() returns.
If FREE() returns 0, then you would need to calculate if you next string
will overflow the current granule. If your current running count is
zero, you have space since you would have got a DF error opening the
file otherwise. If not and your current count is an even granule size,
the disk is full. If not, you calculate the next higher even granule
size from your running count and calculate the difference. If the thing
you're about to write is that size or smaller (including any
terminators, etc.), it fits. Otherwise, the disk is full.
Random files are a bit different. With LOF() and FREE() you can pretty
much guarantee you won't trigger a DF error with some careful
calculations involving the record size. Obviously, writing to a record
<= LOF() will work in this case since it's already allocated and Disk
Basic files can't have "holes".
On 2023-01-26 13:15, Allen Huffman via Coco wrote:
> I know you can use EOF() to know if an input file is at the end, but is there a way to do the opposite, and know if a write would fail? I suppose this is not easy, since it doesn’t know how much you plan to write next.
>
> Without an ON ERROR in CoCo 1/2 BASIC, you would just write until the program crashes with a ?DF ERROR (disk full).
>
> Thoughts?
>
> --
> Allen Huffman - PO Box 7634 - Urbandale IA 50323 - 515-999-0227 (vmail/TXT only)
> http://www.subethasoftware.com - https://www.facebook.com/subethasoftware
>
>
>
More information about the Coco
mailing list