Storing Numeric Data in ASCII Using Tech BASIC: GVG / last revision 30 November 1986 Those of you who have stored numeric data in a file using IPC Tech BASIC have probably had a nasty surprise when you looked in the file to see what was inside. Surprise! It's full of garbage characters! The reason is that Tech BASIC stores the numbers in binary. Not only does this make the file hard to read for YOU, but hard to read for other UNIX programs - and other computers (when you try to transfer data to them). Fortunately, all is far from lost - it is easy to store the numeric data as ASCII strings so that it can be read by other UNIX programs, or (after file transfer) other computers. I ran a few simple BASIC programs to familiarize myself with the process; you may find them informative. I started out with a simple program to generate Fibonacci numbers (you know: 1 1 2 3 5 8 13 21 ... you generate each number in the sequence by adding the previous two together - it demonstrates exponential growth, by the way). The basic, unadorned Fibonacci program (all it does is generate and display the numbers) looks like this: 10 INTEGER first,second,counter,maxcount,temp 20 first=1 @ second=1 @ maxcount=10 ! Initialize variables. 30 ! 40 FOR counter=1 TO maxcount ! Do "maxcount" times. 50 DISP first ! Show the Fibonacci number. 60 temp=first ! Generate the next number. 70 first=first+second 80 second=temp 90 NEXT counter 100 ! 110 END I ran it, and it neatly listed the first ten Fibonacci numbers on the display. I modified the program to store the data as raw binary data in a file (and read it back out again as a check). The program looks like: 10 INTEGER first,second,counter,maxcount,temp 20 first=1 @ second=1 @ maxcount=10 30 ! 40 CREATE "/tmp/junk",1 ! Make a small temporary file. 50 ASSIGN# 1 TO "/tmp/junk" ! Assign a buffer, open file. 60 FOR counter=1 TO maxcount 70 PRINT# 1 ; first ! Store Fibonacci number. 80 temp=first 90 first=first+second 100 second=temp 110 NEXT counter 120 ASSIGN# 1 TO "*" ! Close the file. 130 ! 140 ASSIGN# 1 TO "/tmp/junk" ! Open it again. 150 FOR counter=1 TO maxcount 160 READ# 1 ; temp ! Get Fibonacci number. 170 DISP temp ! Display it. 180 NEXT counter 190 ASSIGN#1 TO "*" ! Close the file again. 200 ! 210 END Note that I must CREATE the file before I use it (specifying a single record in this case); that I use the ASSIGN# statement to connect the file to a buffer; and that I do an ASSIGN# TO "*" to close the file. I ran the program; it stored the data and displayed it again correctly. I looked into "/tmp/junk" - sure enough, it was full of junk. Finally, I modified the same program to do OUTPUT and ENTERS with a file ... a few words of explanation before I continue. The ASSIGN statement assigns an "I/O path" to an interface or file. Each I/O path is represented by a number - either an "interface select code" for an I/O interface (represented by a number from 3 through 10) or a "file selector" (represented by a number from 11 through 20). I can then OUTPUT data to the file just like the file was an output device, and ENTER data from it like it was an input device. The program looks like: 10 INTEGER first,second,counter,fib_number,maxcount,temp @ DIM number$[30] 20 ! 30 maxcount=20 @ first=1 @ second=1 40 ! 50 ASSIGN 11 TO "/tmp/good_stuff" ! Open file - notice: no CREATE. 60 FOR counter=1 TO maxcount 70 OUTPUT 11 ; counter,first ! Store Fibonacci number. 80 temp=first ! Generate next number. 90 first=first+second 100 second=temp 110 NEXT counter 120 ASSIGN 11 TO "*" ! Close the file. 130 ! 140 CLEAR 150 ASSIGN 11 TO "/tmp/good_stuff" ! Open it again. 160 FOR counter=1 TO maxcount 170 ENTER 11 ; fib_number,temp ! Get count and number. 180 DISP fib_number,temp ! Display it. 190 NEXT counter 200 ASSIGN 11 TO "*" ! Close the file again. 210 ! 220 END When I looked in "/tmp/good_stuff", there were the numbers, neat and readable, exactly as they had been dumped to the display window. But suppose somebody wants to input a file and doesn't know how many numbers are in it? Easy. If BASIC tries to ENTER from beyond the end of a file, a timeout error occurs; all I have to do is use an ON ERROR statement to trap the timeout error. The input part of the last routine can be easily modified: 120 ! 130 ASSIGN 11 TO "/tmp/good_stuff" ! Open the file again. 140 ON ERROR GOTO 200 ! Trap timeout error. 150 ! 160 ENTER 11 ; fib_number,temp ! Get count and number. 170 DISP fib_number,temp ! Display it. 180 GOTO 160 ! Get next number. 190 ! 200 OFF ERROR ! Timeout - end error handling. 210 ASSIGN 11 TO "*" ! Close the file. 220 ! 230 END Works like a charm. Not bad for an afternoon's work, huh? (Thanks to Michael for consultation.)