[Gambas-user] File Format Help

Benoît Minisini g4mba5 at gmail.com
Thu Jun 17 22:05:07 CEST 2021

Le 17/06/2021 à 16:46, John Dovey a écrit :
> I hope no-one minds if I ask this here. I'm just really unsure how to do 
> this best in Gambas.
> I want to read and write files that have a (very old) specification 
> <http://ftsc.org/docs/fts-0001.016>
> Essentially, it looks like this:
 > [...]


1) The safest way of reading such file format is opening the file and 
use the READ instruction to read exactly the number of bytes you need 
for each format element.

2) A more elegant way is declaring a structure like you did so that you 
can read the file format in one shot, -but- you *must* understand how 
Gambas datatypes work then!

Read carefully the READ help page, and understand that:

- You can't use a Gambas string for reading a fixed size string, that is 
actually an array of bytes. Once you have an array of bytes, you can 
transform it into a string.

- You can't use a Gambas array for reading an array of native datatypes. 
You have to use embedded arrays (see the documentation page about 
arrays, and the one about structures).

Note that if you use the method 1), the READ instruction has a special 
syntax for directly reading a specific number of bytes and transform 
them into a Gambas string.

Use either method 1) or 2), depending on what you prefer.


Benoît Minisini

More information about the User mailing list