Files – an introduction

Home/Moving on.../Files – an introduction
Files – an introduction 2016-10-25T17:28:33+00:00

Files – reading and writing

So far anything that you have entered into a Small Basic program while it has been executing has been lost as soon as the program ended.  This chapter shows how data can be saved and made available the next time your program needs it.  It is done by reading and writing to FILES.

A FILE is data stored on storage media such as your Hard Disk Drive or Memory Stick. In its simplest form it has no structure – it’s just just a collection of bytes from start to the end of the file.  Unless you know what each byte represents, its difficult to work with.

A more important type of file for us is what is called a TEXT FILE.  These files are still collections of bytes but they are organised into lines containing ASCII or UTF-8 characters that we can read in as individual lines of data.  In many ways it is the equivalent of the ARRAY structure studied in the last chapter but held on your HDD or memory stick.   (In fact the LitDev extension provides a way of reading an entire file into and array).  You can open a file and read in each line of data, using it as required.  You can open a file and write lines of data to it to avoid losing it when the program finishes.

The first example below shows a simple program that reads a 1 line text file to find out how many times the program has been run.  It also writes to the file each time to increment the value stored ready for next time.

FileCount-A

This file uses just Small Basic V1.2 commands.  It starts by specifying a filename to use (in the programs own folder) and then reads in a list of files in that area.  This is done so that it can then see if the file exists.   If it does, then the first line is read and used to display a message.  It also adds 1 to the value stored and writes it back to the file.

If the file does not exist, this must be the first time the program has run so a suitable message is displayed and the files is created with the value ‘1’ as its first line.

FileCount-B

In this example, I have made use of the LDFile.Exists method to find out if the file exists.   This is the only difference between the two versions.

Note that the contents of the file are plain text so can be opened with ‘Notepad’ to examine them.  This is why I used the file extension ‘.txt’ on these files.

Properties & Methods

Box5O

Properties
 These are the properties of the standard File object in Small Basic.

Box5R

Methods
These are the Methods available with the standard File Object in Small Basic.

Files – reading and writing

Below is a second example for you to try, it asks the user to enter five words, saves them and then reads them back.  Enter it, save it and test it. (You can enter any words, or sentences, you want.)

Reading and writing a text file

Short program to demonstrate reading and writing text to a file.

End of Topic Tasks

Explanation

How this program works…

The program first sets up a variable called ‘FileName’.  This holds the full path and name of the file I am going to create. The file is going to be called ‘FileWords.txt’ and will be in the same folder as the program.

Having done this, I call a subroutine that collects five words (or sentences), from the user and writes them to the file.  The screen is then cleared and the final subroutine is called.  This simply reads back the first five lines of the file and displays them on the screen.

This subroutine is only concerned with getting some data form the user of the program.  We want 5 items of data so a counted loop is used:

Line 13: Set up a counted loop with the control variable ‘Line’.

Line 14: Write a prompt ‘Please type a word’ onto the screen leaving the typing cursor on the same line

Line 15: Collect the input from the user and store it in the variable ‘Value’.

Line 16: Write the value in the variable ‘Value’ to the send of the file using the method ‘AppendContents’ – this opens the file and moves to the end so that new values are added at the end.

Line 17: End of the loop.

Using this method means that I don’t need to specify which line to write to – it is the next available line in the file. Also, unlike the TextWindow.WriteLine method, this one takes two parameters – the name of the file to use and the data to be written.

This subroutine is solely concerned with reading the first five lines from the specified file.

Line 21: This sets up a counted loop with the control variable ‘Line’.

Line 22: Read the contents of line using the loop variable – ‘Line’, from the file into the variable ‘Name’.

Line 23: Display the contents of the variable ‘Name’ with the prompt “You entered: “on the screen.

Line 24: End of the loop.

Notice that you have to specify which line you want to read from the file.  This enables us to have what is called ‘Direct Access’ to the file – we specify exactly which line in any order needed.

If you run this version of the program more than once you stop getting back the five words entered on each run.   Can you work out why?  Can you also modify it so that you do only get the first five lines back?  Why not also try to modify it so that you get all the lines back no matter how many times it is run.  (You either need the LitDev extension to do this easily or you have to check for blank lines.)

Again notice that I have made sure that I have used the ‘.txt’ file extension so that I can use NotePad to examine the file outside of Small Basic.  In fact, as you know that the contents are text, it doesn’t really matter as long as I give NotePad the complete filename – it will open it.

Please note that you cannot write directly to a line that is beyond the end of the file: Using File.WriteLine( File, 12, “Help”) will not write to line 12 if there are not already 11 lines in the file.