File write functions not returning bytes actually written

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

File write functions not returning bytes actually written

Post 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).
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

in other words, you can use these commands to write to full disks?
--Kale

Image
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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!
:oops:
Dare2 cut down to size
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Pretty timely reply Dare.

Got some catching up to do? :)

cheers
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

:D

Doh. Or waking up!
Dare2 cut down to size
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Pretty timely reply Dare

:lol:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

Maybe i'm wrong, but FlushFileBuffers(#File) can be helpful?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The PB file functions are buffered. For small writes, WriteFile() is never called
so there is no returnvalue to pass on.
quidquid Latine dictum sit altum videtur
Post Reply