17.07.2013 Views

GWBASIC User's Manual

GWBASIC User's Manual

GWBASIC User's Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

GW-BASIC <strong>User's</strong> Guide<br />

The program in Example 2 reads, sequentially, every item in the file. When all the data has been<br />

read, line 20 causes an "Input past end" error. To avoid this error, insert line 15, which uses<br />

the EOF function to test for end of file:<br />

15 IF EOF(1) THEN END<br />

and change line 40 to GOTO 15.<br />

A program that creates a sequential file can also write formatted data to the diskette with the<br />

PRINT# USING statement. For example, the following statement could be used to write numeric<br />

data to diskette without explicit delimiters:<br />

PRINT#1, USING"####.##,"; A, B, C, D<br />

The comma at the end of the format string serves to separate the items in the disk file.<br />

The LOC function, when used with a sequential file, returns the number of 128-byte records that<br />

have been written to or read from the file since it was opened.<br />

5.2.3 Adding Data to a Sequential File<br />

When a sequential file is opened in O mode, the current contents are destroyed. To add data to an<br />

existing file without destroying its contents, open the file in append (A) mode.<br />

The program in Example 3 can be used to create, or to add onto a file called names. This program<br />

illustrates the use of LINE INPUT. LINE INPUT will read in characters until it sees a carriage<br />

return indicator, or until it has read 255 characters. It does not stop at quotation marks or commas.<br />

Example 3<br />

10 ON ERROR GOTO 2000<br />

20 OPEN "A", #1, "NAMES"<br />

110 REM ADD NEW ENTRIES TO FILE<br />

120 INPUT "NAME"; N$<br />

130 IF N$="" THEN 200 `CARRIAGE RETURN EXITS INPUT LOOP<br />

140 LINE INPUT "ADDRESS? "; A$<br />

150 LINE INPUT "BIRTHDAY? "; B$<br />

160 PRINT#1, N$<br />

170 PRINT#1, A$<br />

180 PRINT#1, B$<br />

190 PRINT: GOTO 120<br />

200 CLOSE #1<br />

2000 ON ERROR GOTO 0<br />

In lines 10 and 2000 the ON ERROR GOTO statement is being used. This statement enables error<br />

trapping and specifies the first line (2000) of the error handling subroutine. Line 10 enables the<br />

file:///C|/Documents%20and%20Settings/Lorenzo/Desktop/GW%20Basic/Chapter%205.html (4 of 9)28/03/2004 21.29.02

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

Saved successfully!

Ooh no, something went wrong!