User Tools

Site Tools


storing_20structures_20in_20files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

storing_20structures_20in_20files [2018/03/31 13:19]
127.0.0.1 external edit
storing_20structures_20in_20files [2018/04/17 18:58] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, July 2006//\\ \\  BBC BASIC for Windows 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 **"​PRINT #​file%,​struct{}"​** and **"​READ #​file%,​struct{}"​**,​ but unfortunately these statements are not syntactically valid.\\ \\  However it is quite straightforward to achieve the same effect using calls to the Windows API. To write an entire structure **struct{}** to the file opened on channel **file%** use the following code:\\ \\  //by Richard Russell, July 2006//\\ \\  BBC BASIC for Windows 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 **"​PRINT #​file%,​struct{}"​** and **"​READ #​file%,​struct{}"​**,​ but unfortunately these statements are not syntactically valid.\\ \\  However it is quite straightforward to achieve the same effect using calls to the Windows API. To write an entire structure **struct{}** to the file opened on channel **file%** use the following code:\\ \\ 
 +<code bb4w>
         SYS "​WriteFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0         SYS "​WriteFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0
 +</​code>​
 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:​\\ \\ 
 +<code bb4w>
         SYS "​ReadFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0         SYS "​ReadFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0
 +</​code>​
 It is best not to mix these statements with conventional BASIC file reads and writes, but if you do you must flush BASIC'​s file buffers before each of the API calls, as follows:\\ \\  It is best not to mix these statements with conventional BASIC file reads and writes, but if you do you must flush BASIC'​s file buffers before each of the API calls, as follows:\\ \\ 
 +<code bb4w>
         PTR#file% = PTR#file%         PTR#file% = PTR#file%
         SYS "​WriteFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0         SYS "​WriteFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0
Line 11: Line 16:
         PTR#file% = PTR#file%         PTR#file% = PTR#file%
         SYS "​ReadFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0         SYS "​ReadFile",​ @hfile%(file%),​ struct{}, DIM(struct{}),​ ^temp%, 0
 +</​code>​
 ==== 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 [[/​Storing%20structures%20containing%20strings|Storing structures containing strings]] for details of an alternative method.  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%20containing%20strings|Storing structures containing strings]] for details of an alternative method.
storing_20structures_20in_20files.txt ยท Last modified: 2018/04/17 18:58 by tbest3112