Chapter 21: Figures, Sounds and Files
21.12. Writing and reading tables to external files

The main use for files is to store and retrieve data, and the most flexible form of data used by Inform is the Table, so facilities are provided which make it as easy as possible to write and read the contents of a table to files.

To save the contents of a table to a file, we use the phrase:

write File of Glaciation Data from the Table of Antarctic Reserves

naming the file (which should be one already declared) and specifying which table it should come from. To load a file back into a table,

read File of Glaciation Data into the Table of Antarctic Reserves

We can check if a file already exists using:

if File of Glaciation Data exists, ...

On writing a table, any blank rows are automatically moved to the bottom, and only the non-blank rows are written; on reading a file back in, any rows left spare at the foot of the table are automatically blanked. On the other hand if the file is too large to fit into the table - with too many columns or too many rows - a run-time problem is produced.

One unfortunate restriction must be kept in mind. Some of what is stored in tables is solid information whose meaning never changes: the number 342, for instance, means the same to everyone. But other information depends entirely on the current location of certain structures in memory - for instance, a rule is internally referred to by its memory location. This potentially changes each time Go or Replay is clicked, and so it is not safe to pass it from one copy to another, or from one project to another. The only tables which Inform allows us to write into files are those containing "safe" data: numbers, units, times of day and kinds of value with named alternatives. Scenes, rules or rulebooks, in particular, are not allowed.

And nor is text - but we can easily get around that, because indexed text (see the previous chapter) is. For instance, the following table can legally be written out to a file, weareas it would have thrown up a run-time problem if we tried it with the surface column defined simply as text:

Table of Neptune's Moons
moon   surface (indexed text)   
1   "utterly unknown"   
2   "cryovolcanic ridges"   
3   "highly irregular and sooty"   


413
* Example  Alien Invasion Part 23
Keeping a preference file that could be loaded by any game in a series.

RB
414
** Example  Labyrinth of Ghosts
Remembering the fates of all previous explorers of the labyrinth.

RB
415
*** Example  Rubies
A scoreboard that keeps track of the ten highest-scoring players from one playthrough to the next, adding the player's name if he has done well enough.

RB


PreviousContentsNext