Home | About Us | In-Circuit Test | Functional Test | Tutorials, Software, etc | Fixtures | Contact Us
Hartman Associates
BT Basic File I/O

Learn how to do simple file read/write operations in BT Basic

Reading a text file using BT Basic

BT Basic variables and file I/O are limited to a maximum length of 80 characters. The line numbers shown in the format #### : are used for reference only and are part of the BT Basic program.

Program Listing #1

0001: Test file line 1
0002: Test file line 2
0003: Test file line 3
0004: Test file line 4
0005: Test file line 5

Program Listing #1 is for the file named "test.txt" and is used as the file targeted for reading in the following examples.

Program Listing #2

0001: assign @Rd to "test.txt";read,exclusive
0002:
0003: loop
0004: enter @Rd;T$
0005: print T$
0006: end loop
0007:
0008: assign @Rd to *

Program Listing 2 is our BT Basic program for reading text files. (see Listing 2 below)

Line 0001 assigns a file node name to the file named "test.txt" for read access (see Listing 1).
Line 0003 start a program loop.
Line 0004 uses the file read command "enter" to read the next line of the file into variable "T$".
Line 0005 displays "T$" on the terminal screen
Line 0006 is the properly nested "end loop" statement corresponding to the "loop" statement in line 0002.
Line 0008 closes the file node name associated with "test.txt".

Running the program will yield a result similar to Screen Shot 1. Please note that the program terminated abnormally due to the fact we have a limited size file and an infinite loop with no exit condition. The error generated by the program is presented on the BT Basic Window Status Line F1007 An end-of-line was found on the read at line 4.

Screen Shot #1

ss1.jpg

This message is derived from a BT Basic built in function variable name errm$ which contains the message associated with the last error that occurred. Another variable available for determining the cause of the last error is the associated integer variable errn. In this case typing errn followed by the return key would present the following error number on the terminal screen.

101007

Program Listing #3

0001: assign @Rd to "test.txt";read,exclusive
0002:
0003: loop
0004: enter @Rd,,Error;T$
0005: exit if Error
0006: print T$
0007: end loop
0008:
0009: assign @Rd to *

Now we know what happens when we read past the end of a file and can use that information to design a graceful exit from our file reading program. Refer to Listing 3. Note the modified statement in line 0004, "enter @Rd,,Error;T$" and the addition of the statement in line 0005, "exit if Error" The modified statement in line 0004 traps the error and line 0005 provides an exit condition for the loop. Virtually any BT Basic error may be trapped and processed appropriately. The programmer might want to only exit if the error was a 101007 end of file error, but process other file I/O errors differently.

Program Listing #4

0000: assign @Rd to "test.txt";read,exclusive
0001:
0002: loop
0003: T$=""
0004: enter @Rd,,Error;T$
0005: exit if Error
0006: print T$
0007: end loop
0008:
0009: assign @Rd to *

While our program terminated normally, a careful look at the terminal may show that the last line of the text file was displayed on the terminal twice. If this happened it is caused by a blank line at the end of the file. If you insert a blank line below line 0002 in the text file "test.txt" and run Listing 3 this phenomenon is demonstated. Your terminal should look line Screen Shot 2. Note that the statement "Test file line 2" appears twice. This occurs because a blank line does not replace any existing text in the variable "T$". This can create havoc when parsing a file and writing it to another file. Refer to Listing 4 for the solution. The statement in line 0004, T$="" solves this problem by clearing the string variable before each read. With the addition of this last line of code, We have a program that displays exactly what the read file contains as in Screen Shot 3.

Screen Shot #2

ss2.jpg

Screen Shot #3

ss3.jpg

Writing a File in BT Basic

Listing 5 is a modified version of Listing 4. Line 0002 assigns a file node for writing to a file name "temp.txt". Note that the "over" attribute causes the file to be overwritten if it already exists. Line 0006 is modified to write to the file node named "Wr", rather than print to the display terminal and Line 0008 closes the file node. The written file should be identical to the read file upon program completion.

Program Listing #5

0000: assign @Rd to "test.txt";read,exclusive
0001: assign @Wr to "temp.txt";write,over
0002: loop
0003: T$=""
0004: enter @Rd,,Error;T$
0005: exit if Error
0006: output @Wr;T$
0007: end loop
0008: assign @Wr to *
0009: assign @Rd to *

Copyright © 2005 Hartman Associates