Page 1 of 1
Is there error checking with file writing functions?
Posted: Fri Jun 29, 2007 8:47 pm
by Osmiroid
For the following functions:
WriteByte
WriteCharacter
WriteData
.
.
.
WriteStringN
WriteWord
Is there any way of checking whether the write was successful? I'm using a demo version of PureBasic.
Posted: Fri Jun 29, 2007 9:10 pm
by netmaestro
Those commands are returning 1 when successful, so a reasonable assumption would be they'd return 0 if not. But the doc doesn't say so and I don't know how you'd test it.
Posted: Fri Jun 29, 2007 9:39 pm
by Trond
It's not supposed to fail as long as your file is opened correctly.
Posted: Fri Jun 29, 2007 10:00 pm
by Osmiroid
netmaestro wrote:Those commands are returning 1 when successful, so a reasonable assumption would be they'd return 0 if not. But the doc doesn't say so and I don't know how you'd test it.
It doesn't seem to work. I set up a small test program and supposedly it wrote a 6 meg file to a floppy without complaining.

Posted: Fri Jun 29, 2007 11:23 pm
by Tranquil
WriteData() etc returns the number of bytes written to the disc. it works fine here.
Posted: Sat Jun 30, 2007 12:09 am
by Osmiroid
Tranquil wrote:WriteData() etc returns the number of bytes written to the disc. it works fine here.
The problem is that all the write functions report bytes supposedly written even after the drive is full.
File Monitor was returning "DISK FULL" when WriteString was reporting bytes being written.
Posted: Sat Jun 30, 2007 12:31 am
by Kaeru Gaman
just an idea:
does a FlushFileBuffers() report a full disk?
Posted: Sat Jun 30, 2007 1:37 am
by Osmiroid
Kaeru Gaman wrote:just an idea:
does a FlushFileBuffers() report a full disk?
Ah! Now that's interesting, FlushFileBuffers() returns 0 as soon as the disk reports full.
Posted: Sat Jun 30, 2007 9:39 am
by Kaeru Gaman
ok.
so, the write-command does not report an error, because non happenes,
because the writing to the Buffer indeed was successful.
only forcing the buffer to write to the media will finally report the error.
not the writing-command itself, but the processing of the buffer is physical writing nowadays...

Posted: Sat Jun 30, 2007 9:56 am
by Psychophanta
It seems a Microsoft issue (or bug ?) :roll:
Posted: Sat Jun 30, 2007 2:13 pm
by Osmiroid
Psychophanta wrote:It seems a Microsoft issue (or bug ?) :roll:
I don't know if I should report this as a bug because the return values aren't specified in the documentation.
Posted: Mon Jul 02, 2007 4:44 am
by Rescator
I suggest you report it as a bug,
if it is a PB bug or a issue that the PB team can fix/handle they will,
If not then maybe a note in the documentation is needed.
And when I mean a "issue" the PB team can fix,
like mean possibly checking/keeping track of free disk space per file(/drive?) updating the value each time the buffer is flushed and reduce the "space free" value each time something is written to the buffer.
I'm not sure how PB's filebuffer is implemented but a solution like that might help catch an issue like this a bit earlier than currently for example.
It is still not possible to truly eliminate this issue though, due to the nature of filebuffering. If this is a common issue with your program(s) I suggest disabling buffering for the files you write and either use your own filebuffering or do not use filebuffering at all. (writes and reads will be a lot slower though)
Using a smaller filebuffer value might also work, but again the speed issue.
Another issue with fixing this is that if multiple files are written at the same time the free space values may get out of sync between buffer flushing.
Again I got no idea how filebuffers are implemented in PB so this may or may not be easy to "fix".
PS! Normally you should always know more or less how much data you need to write to disk, so keeping track of values yourself should be not that hard.
It is also adviced that you set the filebuffer size to match the data blocks you write.
So if you write 16KB each time you write data then a filebuffer of 16KB might be best, then do a flushbuffer right after.
(actually in this case, disabling filebuffer for that file and writing the data directly might be best, filebuffers work best when you do not always know how muc data is read or written each time or when you know a lot of small reads or writes will happen. especially ReadByte and WriteByte and similar benefit a lot from some form of filebuffer.)
Re: Is there error checking with file writing functions?
Posted: Mon Jul 02, 2007 10:09 pm
by PB
> Is there any way of checking whether the write was successful?
http://www.purebasic.fr/english/viewtopic.php?t=11902