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:
Open File "<file name>" As <ReadOnly or ReadWrite> Using <file handle>
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.
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.
Global GETSTRING$
DefProc INIT_DISPLAY()
DefProc READ_FILE()
INIT_DISPLAY()
READ_FILE()
Text(100,100,GETSTRING$)
Update Display
Wait Key
Procedure INIT_DISPLAY()
Create Map 640,480,1,1 In Bank 50
Set Tile 0,0,1 Using Bank 50
Create Plane 1 Using Bank 50
ReDirect GDI Output To Bank 1
EndProc
Procedure READ_FILE()
Local X$
Local QUIT
QUIT = False
Open File "text.txt" As ReadOnly Using 1
Repeat
X$ = FileRead$(1,2)
If X$ = "&" Then
Else
GETSTRING$ = GETSTRING$ + X$
EndIf
Until QUIT = True
Close File 1
EndProc
EndProg
|