storing_20structures_20in_20files
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
storing_20structures_20in_20files [2018/03/31 13:19] – external edit 127.0.0.1 | storing_20structures_20in_20files [2024/01/12 22:46] (current) – Add generic code (e.g. for BBCTTY) richardrussell | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Storing structures in files===== | =====Storing structures in files===== | ||
- | //by Richard Russell, July 2006//\\ \\ BBC BASIC for Windows | + | //by Richard Russell, July 2006//\\ \\ BBC BASIC does not provide any built-in means of writing entire data structures to a file and reading them back again. Ideally it would be nice if one could say **" |
- | SYS " | + | BB4W: |
+ | <code bb4w> | ||
+ | | ||
+ | </ | ||
+ | BBCSDL: | ||
+ | <code bb4w> | ||
+ | SYS " | ||
+ | </ | ||
+ | Generic (e.g. BBCTTY): | ||
+ | <code bb4w> | ||
+ | DEF PROCwritestruct(F%, | ||
+ | LOCAL s$ : PTR(s$)=s{} : !(^s$+4)=DIM(s{}) : BPUT#F%,s$; : !(^s$+4)=0 | ||
+ | ENDPROC | ||
+ | </ | ||
Similarly to read an entire structure **struct{}** from the file opened on channel **file%** do the following: | Similarly to read an entire structure **struct{}** from the file opened on channel **file%** do the following: | ||
- | | + | BB4W: |
+ | <code bb4w> | ||
+ | | ||
+ | </ | ||
+ | BBCSDL: | ||
+ | <code bb4w> | ||
+ | SYS " | ||
+ | </ | ||
+ | Generic (e.g. BBCTTY): | ||
+ | <code bb4w> | ||
+ | DEF PROCreadstruct(F%, | ||
+ | LOCAL s$ : PTR(s$)=s{} : !(^s$+4)=DIM(s{}) : s$=GET$#F% BY LENs$ : !(^s$+4)=0 | ||
+ | ENDPROC | ||
+ | </ | ||
It is best not to mix these statements with conventional BASIC file reads and writes, but if you do you must flush BASIC' | It is best not to mix these statements with conventional BASIC file reads and writes, but if you do you must flush BASIC' | ||
+ | <code bb4w> | ||
PTR#file% = PTR#file% | PTR#file% = PTR#file% | ||
SYS " | SYS " | ||
Line 11: | Line 38: | ||
PTR#file% = PTR#file% | PTR#file% = PTR#file% | ||
SYS " | SYS " | ||
+ | </ | ||
==== Important note: ==== | ==== Important note: ==== | ||
This technique does **not** work for structures containing strings or string arrays. In that case it is probably easier simply to write the individual structure members to file one at a time and read them back again. However see the article [[/ | This technique does **not** work for structures containing strings or string arrays. In that case it is probably easier simply to write the individual structure members to file one at a time and read them back again. However see the article [[/ |
storing_20structures_20in_20files.1522502385.txt.gz · Last modified: 2024/01/05 00:16 (external edit)