07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12. Using and Storing Data 85<br />

———————————————————————————————————————<br />

x The data-file lines must exactly match the INPUT requests as the program<br />

cannot "re-ask" a file for input.<br />

———————————————————————————————————————<br />

If there are too few or too many items, or the types do not match, your program will stop<br />

with an error. If you can't fit all required input items on one line (as with the last question),<br />

you can end a line with a comma to indicate that another input item follows on the next line.<br />

Use the arrow keys to move to the end of the TRIVDATA file and you'll see that the last<br />

line of data is the last line of the file. There are no extra CR or CR-LF sequences at the end<br />

of the file. (If a data file ends with a blank line, you may receive an error message such as<br />

"Too few input items" when <strong>True</strong> <strong>BASIC</strong> expects more data but finds no input items on the<br />

line.)<br />

You may also use the LINE INPUT, MAT INPUT, and MAT LINE INPUT statements to<br />

read from text files. LINE INPUT is, in fact, the best statement to use with strings that<br />

might have commas or quotes in them; see the section "Using LINE INPUT with String<br />

Data in Text Files" below. Just be sure that the data in the file matches the appropriate format<br />

for the input statement or statements in the program. (The MAT statements read into<br />

arrays and are explained in the next chapter.)<br />

Creating Text Files<br />

You may use <strong>True</strong> <strong>BASIC</strong>'s screen editor to enter data into a text file. Create a new file as<br />

if you were creating a new program, and then type in your data in the proper format. Do not<br />

use any DATA statements – and of course no line numbers!<br />

You can also create data files with any application (such as a word processor, spreadsheet,<br />

or database program) that lets you save text-only files. Check the instructions for your application<br />

to learn how to save such files; put commas between data items if necessary.<br />

For practice, create an alternative set of questions for the TRIVIA2 program. You can then<br />

edit TRIVIA2 to open you new data file, or you can modify the program to ask you what file<br />

to use for input:<br />

INPUT PROMPT "What file contains the questions?": filename$<br />

OPEN #1: name filename$<br />

<strong>True</strong> <strong>BASIC</strong> programs can also create text files and put data into them, as described in the<br />

next section.

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

Saved successfully!

Ooh no, something went wrong!