problem with binary read/write

New to FreeBASIC? Post your questions here.
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: problem with binary read/write

Postby fxm » Apr 24, 2018 19:23

In documentation, see:
- Field
- Structure packing/field alignment
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: problem with binary read/write

Postby paul doe » Apr 24, 2018 19:39

MrSwiss wrote:Sorry to butt in on this, but using Integer (in UDT), for file access (any method),
is actually *asking for troubles*, because SizeOf(Integer) varies, with the used
version of FBC (32/64 bit's), making the file unusable, when accessing with a
.exe compiled with the *other* FBC!

No problem, as you're dead right. As I already stated, I coded it in a hurry =D
sero wrote:@paul doe - I didn't know about field alignment until just now. I don't understand the purpose of this. The manual talks about padding which doesn't make any sense to have padding in a binary file. I changed the field = to 1, then 2, then 4 and the file size was not impacted at all. So what exactly does this do?

Effectively, padding in a file doesn't make any sense, only in memory (memory aligned read/writes are faster; also, some architectures don't even support reading from unaligned addresses), but you DO have to take it into account when reading/writing to a file. Another thing you need to take into account is the order of the elements of a structure:

Code: Select all

'' Size of the type is 16 bytes, right?
type myType1
   as ubyte a, b, e, f
   as double c
   as long d
end type

? sizeOf( myType1 ) '' Wrong!

type myType2
   as long c
   as ubyte a, b, e, f
   as double d
end type

? sizeOf( myType2 ) ' ' More like it

sleep()

As you can see, the order can determine the true size of a record in memory, see here: https://wiki.sei.cmu.edu/confluence/display/c/EXP03-C.+Do+not+assume+the+size+of+a+structure+is+the+sum+of+the+sizes+of+its+members. The discussion is about C, but FreeBasic also applies. That's why is very important not only to preserve the size of the elements but also the order of them when you're reading, for example, a file header. Little details that can save you a world of trouble ;)
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: problem with binary read/write

Postby paul doe » Apr 24, 2018 20:18

fxm wrote:In documentation, see:
- Field
- Structure packing/field alignment

fxm, is this normal behavior? I mean, writing to an arbitrary position into a binary file opened with 'access write' wouldn't just write the bytes you specify, where you specify, instead of zeroing everything up to that point? Or I am missing something? I've had to open it as 'access read write' to pull this off without erasing all the data before the position I specified. I don't recall having this problem before...
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: problem with binary read/write

Postby fxm » Apr 24, 2018 20:44

See this topic: Set EOF
Consequently, I updated the documentation of: Open
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: problem with binary read/write

Postby paul doe » Apr 24, 2018 20:55

I see, it's a file system quirk, not a FB bug. Thank goodness ;)
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: problem with binary read/write

Postby paul doe » Apr 24, 2018 20:57

jj2007 wrote:We have treated these issues in the Set EOF thread just a few weeks ago.

Sorry, mate. Didn't see this post until now =D
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: problem with binary read/write

Postby fxm » Apr 24, 2018 21:07

paul doe wrote:I don't recall having this problem before...

To my knowledge, there has never been a change in the compiler on this behavior.
paul doe
Posts: 804
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: problem with binary read/write

Postby paul doe » Apr 24, 2018 22:12

fxm wrote:To my knowledge, there has never been a change in the compiler on this behavior.

That's ok, perhaps it was on another language and I don't remember which. Thanks.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests