07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

226 BRONZE <strong>Edition</strong> <strong>Guide</strong><br />

Here’s another example that changes all letters in a file to lowercase:<br />

DIM line$(1000)<br />

OPEN #3: NAME “Program5.Tru”<br />

LET i = 0<br />

DO WHILE MORE #3 ! Read lines into array<br />

LET i = i + 1<br />

LINE INPUT #3: line$(i)<br />

LOOP<br />

ERASE #3<br />

! Erase the file<br />

FOR j = 1 to i<br />

! Rewrite in lowercase<br />

PRINT #3: Lcase$(line$(j))<br />

NEXT j<br />

END<br />

The program reads the file into an array, erases the file, and then writes lowercase versions<br />

of the lines back into the file.<br />

A word of caution about using the MAT PRINT and MAT INPUT statements with text files:<br />

while both work with text files, the MAT PRINT statement does not write information in a<br />

format that will work with the MAT INPUT statement. The MAT INPUT statement<br />

expects items of a row to be separated by commas, but the MAT PRINT statement separates<br />

the items of a row by spaces. There are two ways to solve this problem:<br />

(1) Create the file’s contents by printing individual elements, putting a comma after each<br />

item except the last:<br />

...<br />

FOR i = 1 to Ubound(array) - 1<br />

PRINT #7: array(i); “, “;<br />

NEXT i<br />

PRINT array(Ubound(array))<br />

...<br />

(2) Use the LINE INPUT statement to input an entire line from the file and then “parse”<br />

the line into its component items using the ExplodeN subroutine provided in the Str-<br />

Lib library.<br />

LIBRARY “C:\TBSilver\TBLIBS\STRLIB.TRC” ! Use appropriate<br />

path name<br />

...<br />

LINE INPUT #4: line$<br />

CALL ExplodeN(line$, array(),” “)<br />

...<br />

You should also be cautious when printing strings to text files for later input. Remember that<br />

the INPUT statement requires double quotes around strings containing commas or leading

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!