Tutorial 5 Part 1

In this tutorial we will look at file handling; reading from and writing to files. This is quite simple to do and useful for saving game states or high scores and reading in level layouts, etc.

In part 1 of this tutorial we will read the contents of a text file.

Before we can do anything to a file we must first open it. For this, we use the following command:

The file handle can be any integer number. Whenever you want to refer to this file you will use this number.

Now you can access the data inside the file. There are various commands for reading in data:

FileRead$(Handle,Bytes)
Reads in the specified number of bytes from the file unless the end of the line of the end of the file is reached. The result of this command is a string.

FileReadBinary$(Handle,Bytes)
As above, except that null characters are interpreted as terminators. The result of this command is a string.

FileReadDelimited$(Handle,Delimiter)
This reads from the file until the specified delimiter (normally a comma or semicolon) or until the end of the file is reached. The result of this command is a string.

FileReadByte(Handle)
This reads in a single byte from the file as an integer. This means that you can read in special characters and control codes.

In this tutorial, we will use the FileRead$ command.

  • Create a new project with a blank background.

  • Enter the commands for initialising the display and include the "Redirect GDI Output to Bank 1" command so that we can type text to the screen.

  • Create a text file in the directory where your project is located. Type some text into the file and type a "&" at the end of the text and save the file as "Text.txt".

    The reason why you must put a "&" at the end of the file is so that we know when we are at the end of the file. There is a function called EOF (end of file) that should do this automatically but there appears to be a bug preventing it from working. You don't have to use a "&" it can be any character that you are not likely to use it the body of your file.

  • Define a string variable called Getstring$. We will put the contents of the file into this variable.

  • Use the Open File command as demonstrated above.

  • Create a loop that reads the next character from the file into a temporary variable.

      X$ = FileRead$(1,2)
    Note that the number of bytes is set to 2 instead of 1. This appears to be another minor bug. Just enter a number that is one more than the number of characters you actually want, eg. if you want the next 3 characters enter 4.

  • After reading in the next character check that it isn't our end of file character (&) and if it isn't append the character to our Getstring$ variable.

  • Make the loop stop if we reach our end of file character and then print the contents of Getstring$ onto the screen.

    When you run the project you should see the text you entered into the file printed onto the screen. If not then compare your code to the code I have created below.

    NOTE: If you do not wish to type the code below you may download or open this text file and simply copy and paste the code into your project.


    //TUTORIAL 5 PART 1

    Global GETSTRING$ //USED FOR STORING THE CONTENTS OF THE FILE

    //DEFINE PROCEDURES
    DefProc INIT_DISPLAY()
    DefProc READ_FILE()

    //CREATE THE SCREEN
    INIT_DISPLAY()

    //READ IN THE CONTENTS OF THE FILE
    READ_FILE()

    Text(100,100,GETSTRING$) //PRINT THE FILE CONTENTS

    Update Display

    Wait Key //WAIT UNTIL A KEY IS PRESSED BEFORE CONTINUING

    //THE INIT_DISPLAY PROCEDURE CREATES A SCREEN AND A PLANE
    Procedure INIT_DISPLAY()

      Create Map 640,480,1,1 In Bank 50 //CREATE A MAP IN BANK 50
      Set Tile 0,0,1 Using Bank 50 //CREATE A TILE USING A BACKGROUND IMAGE IN BANK 1
      Create Plane 1 Using Bank 50 //CREATE PLANE 1 USING THE MAP IN BANK 50
      ReDirect GDI Output To Bank 1 //ALL GDI OUTPUT (IN THIS CASE TEXT, GOES TO BANK 1)

    EndProc

    Procedure READ_FILE()

      Local X$ //GETS THE NEXT CHARACTER IN THE FILE
      Local QUIT //SET TO TRUE IF THE LOOP SHOULD BE EXITED

      QUIT = False

      //OPEN THE TEXT FILE
      Open File "text.txt" As ReadOnly Using 1 //1 IS THE FILE HANDLE

      Repeat //REPEAT UNTIL QUIT IS TRUE

        X$ = FileRead$(1,2) //READ NEXT CHARACTER IN FILE AND STORE IN VARIABLE

        If X$ = "&" Then //IF WE HAVE REACHED THE END CHARACTER THEN QUIT LOOP
          QUIT = True
        Else
          GETSTRING$ = GETSTRING$ + X$ //ADD THIS CHARACTER TO OUR STRING
        EndIf

      Until QUIT = True

      Close File 1 //CLOSE THE TEXT FILE

    EndProc

    EndProg


  • Here is a useful tip -- As I mentioned before, reading in files is very useful for reading in level data. If you have a game built up of pieces or tiles, create a text file for each level of your game. Create the levels out of numbers with each number representing a type of piece or tile. For example in a Boulderdash game you could have 1 represent a rock, 2 represent earth, 3 represent the player and so on.

    Read the text file into an array which will store the data like a map. You can now place the appropriate sprites depending on the value in the array.

    Next Page