Page 1 of 1
File write functions not returning bytes actually written
Posted: Mon Jul 02, 2007 8:07 pm
by Osmiroid
WriteByte
WriteCharacter
WriteData
WriteDouble
WriteFloat
WriteLong
WriteQuad
WriteString
WriteStringN
WriteWord
The return values aren't described in the docs but I assume that they indicate bytes actually written.
All the above functions indicate bytes written to a file even when they are not (e.g., full drive).
Posted: Mon Jul 02, 2007 8:14 pm
by Kale
in other words, you can use these commands to write to full disks?
Posted: Mon Jul 02, 2007 8:18 pm
by freak
Actually the returnvalues have no meaning at all (thats why there is nothing in the help about them)
Since the file operations are buffered in memory, it cannot be known wether a write
to disk will succeed for this particular operation.
Checking the free disk space for each file operation would be a huge slowdown
and defeat the whole purpose of the buffering.
I would suggest to simply write the whole file and check at the end if the actual
filesize matches what you tried to write. For very large files, a check of the free
disk space inbetween might make sense as well.
Posted: Mon Jul 02, 2007 10:06 pm
by PB
> I would suggest to simply write the whole file and check at the end if the
> actual filesize matches what you tried to write
I posted an example of this technique way back in 2004:
http://www.purebasic.fr/english/viewtopic.php?t=11902
Posted: Tue Feb 05, 2008 4:08 am
by Dare
If you are encountering full disk or bad sector or whatever probs, perhaps something like this from time to time (and assuming the file write action has extended the file) -
Code: Select all
WriteStringN(1,"ABCDEF") ; or whatever write
If Lof(1) <> Loc(1)
debug "Ah cr@p"
EndIf
Not sure if buffering skews this, though. If so, then perhaps flushbuffer first? You probably wouldn't want to do this every write, just from time to time.
Edit:
Lol, that is exactly what your code does, PB! I should have read your link first!

Posted: Tue Feb 05, 2008 4:47 am
by rsts
Pretty timely reply Dare.
Got some catching up to do?
cheers
Posted: Tue Feb 05, 2008 8:44 am
by Dare
Doh. Or waking up!
Posted: Tue Feb 05, 2008 9:48 am
by PB
> Pretty timely reply Dare

Posted: Tue Feb 05, 2008 7:43 pm
by eJan
Maybe i'm wrong, but FlushFileBuffers(#File) can be helpful?
Posted: Tue Feb 05, 2008 8:52 pm
by Rescator
@freak
Any chance of changing the behavior of the write functions so that they return #False if writing failed for some reason? and #True if it succeeded?
Win32 API
WriteFile()
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
I assume WriteFile() is what PureBasic uses on i.e. Windows, so merely passing along the return vals on writes would be very easy I hope.
No idea on Linux or Mac but I assume it's very similar?
Posted: Tue Feb 05, 2008 9:00 pm
by freak
The PB file functions are buffered. For small writes, WriteFile() is never called
so there is no returnvalue to pass on.