Is there error checking with file writing functions?

Just starting out? Need help? Post your questions and find answers here.
Osmiroid
User
User
Posts: 10
Joined: Fri Jun 29, 2007 6:11 pm
Location: Canada

Is there error checking with file writing functions?

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It's not supposed to fail as long as your file is opened correctly.
Osmiroid
User
User
Posts: 10
Joined: Fri Jun 29, 2007 6:11 pm
Location: Canada

Post 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. :D
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

WriteData() etc returns the number of bytes written to the disc. it works fine here.
Tranquil
Osmiroid
User
User
Posts: 10
Joined: Fri Jun 29, 2007 6:11 pm
Location: Canada

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

just an idea:
does a FlushFileBuffers() report a full disk?
oh... and have a nice day.
Osmiroid
User
User
Posts: 10
Joined: Fri Jun 29, 2007 6:11 pm
Location: Canada

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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... ;)
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

It seems a Microsoft issue (or bug ?) :roll:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Osmiroid
User
User
Posts: 10
Joined: Fri Jun 29, 2007 6:11 pm
Location: Canada

Post 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.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Is there error checking with file writing functions?

Post by PB »

> Is there any way of checking whether the write was successful?

http://www.purebasic.fr/english/viewtopic.php?t=11902
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply