First Steps in Programming
RISC OS Computers
Martyn Fox

Chapter 4 : Saving and Loading

We've now seen how to enter programs and run them, and hopefully you will have tried a few experiments of your own. Please do - it's the best way to explore the possibilities of your machine!

So far, we've not covered saving a program, so that you can run it again, or writing a program using a text editor, such as Edit, Zap or StrongEd.

Using a Text Editor

Writing a program using a text editor is a little different from doing it using Basic's command level. The first major difference is that you won't see any line numbers, unless you choose to have them visible. When you save the program, the editor will number all the lines with a regular interval between them so, if you load a program, add or remove lines, then resave it, the new version will have the lines numbered differently.

This is a good reason not to use a GOTO command. If you were to add an extra line in immediate mode (as we did earlier, when we added a line 15 between lines 10 and 20), then renumbered the program using the RENUMBER command, any references to line numbers, such as a GOTO command, would automatically be updated. A text editor will not do this, though, so the program would not work properly.

Using structures such as loops and conditional execution, which we found out about in the previous section, you should never have to use GOTO again.

We won't deal with the exact techniques for using each type of text editor; see the editor's instructions for that. Where we mention basic principles, though, we'll describe Edit because that is available on every RISC OS computer.

Now let's create a new program, using the editor. First follow its instructions to create a Basic file. If you're using Edit, open its icon bar menu, go to the Create submenu and select Basic. A window will appear.

Now type in the following:

    What shall I print",a$
    FOR n%=1 TO LENa$
      PRINT LEFT$(a$,n%)

It's up to you whether or not you add an extra couple of spaces to indent the third line.

When you've finished, you can save the program in the usual RISC OS fashion, by either pressing F3 or using the menu to get the 'Save' dialogue box. Assuming that you created a Basic file, rather than a text file, you should see a Basic filetype icon in the dialogue box. If not, it's not too late to change the filetype, using the editor's Set type menu option, provided you do it before you save the file. Set the filename to 'What' and drag the icon to the directory of your choice.

A Basic filetype icon should appear in the window, representing your file. You can double-click on this icon to run your program. A window will appear in the middle of the screen (called a Command Window) and your program will run in it.

When you type in a string and press Return, it will appear one character at a time, as n% increases until it reaches the number of characters in the string, like this:

    What shall I print?ABCDEFGH

If you close the window in which you wrote your program, the editor will, of course, forget about it. To load it again, hold down Shift while you double-click on the file icon.

This actually applies to any type of file. If you Shift-double-click on its icon, it will be loaded into your text editor so that you can see its contents.

Trouble-Shooting Trouble

Now try adding an extra line to the program so that it looks like this:

    INPUT "What shall I print",a$
    FOR n%=1 TO LENa$
      PRINT LEFT$(a$,n%)

The new line just has a letter Z on it which is the easiest way of producing an error!

If you had typed this program in using Basic's command level and run it by typing RUN, you would expect to see an error message:

    Mistake at line 20

If you run it by double-clicking on the file icon, though, all you'll get is an error message saying:


which is not a lot of help in finding the error (except in a program as short as this one!)

Error Handling

You can deal with this problem by incorporating an error handler into your programs. This consists of the keywords ON ERROR, followed by instructions for the program to follow. To make it tell you the line number, the error handler line would be:


You would put this line at a very early point in your program, so that the program would encounter it before it gets to any errors (it can't use it if it doesn't know it's there!).

When Basic detects an error, it jumps to the start of this line. The keyword REPORT makes it print the error message. The next command adds the words 'at line', followed by the line number, which it gets from the Basic variable ERL. You will probably want the program to stop at this point, so we add an END which stops the program so that it doesn't plough straight onto the next line (which may well contain the error, sending it straight back to this line again!).

Adding this line to the beginning of the program makes it look like this:

    INPUT "What shall I print",a$
    FOR n%=1 TO LENa$
      PRINT LEFT$(a$,n%)

This time, if you run the program, the error message will read:

    Mistake at line 30

(The error is now on the third line of the program, not the second because you've added a line!)

If you get a message telling you that there is an error at, say, line 3020 of your very long program, you might well say to yourself, "How do I find line 3020 when I can't see any line numbers?" The text editor will have a facility for finding a particular line (if you are using Edit, key F5 to open Edit's 'Go to text line' dialogue box) but only by reference to which line it is, not which line number it is.

The secret is to ensure that you know how the editor numbers the lines of your program. If you have it set so that it starts with line 10 and numbers the lines in tens, line 3020 will be the 302nd line in the program.

All the listings in the rest of this guide will show the line numbers to make it easier to refer to them in the text, but you can, of course, enter them in a text editor without line numbers. They will all have error handlers like the one above to make it easier to debug them while editing. The listings will also be shown with spaces and indentations for clarity.

Loading and Saving in Immediate Mode

You may occasionally want to be able to load a program into Basic in its immediate mode and save it again, possibly as an aid to debugging.

You can load a program with the command LOAD, but you have to tell the system where it is, as well as its filename.

Outside the desktop, the filing system makes one directory the Currently Selected Directory or CSD for short. Normally this will be the root directory of your hard disc. The root directory always has the name '$' and no other directory may include this symbol in its name.

If you type *Cat as we did right at the beginning of our experiments, the machine lists the contents of the CSD.

If your file is in the CSD, you can load it with the command:

    LOAD "What"

or with whatever the filename is between the quotes. You can then LIST or RUN it.

If the file isn't in the CSD, you could either enter the full pathname of the file between the quotes or change the CSD to the directory containing the file. This is easy if you have RISC OS 4; just click Menu in the directory window and select Set work directory. If you do not have RISC OS 4, you can change the CSD with the *Dir command.

To save a file in the same manner, type:

    SAVE "What"

or put the full pathname between the quotes.

There is an easier way, though. Add the following line as the first line of the program:

    10 REM > What

The keyword REM is short for REMark. What follows it is usually a comment for the benefit of anybody reading and trying to understand the program. When Basic comes across a REM, it usually ignores the rest of the line.

In this case, though, it makes an exception. If you type SAVE on its own (or even just SA.) without a filename, Basic looks at the first line of the program, checking for a REM keyword, followed by a '>' character and treats what follows it as a filename or pathname.

From now on, we will normally show programs with embedded filenames in the first line, showing the file being saved in the Currently Selected Directory. It's up to you to make any changes you need.

previousmain indexnext

© Martyn & Christine Fox 2003