You are on page 1of 3

DIM seenError% ' tells if error "Input past end of file" occurred CLS q$ = CHR$(34) ' the double-quote

character (") ' Create a sequential data file with a missing value. ' The data also contains other minor problems that parseNumber ' and parseString handle better than INPUT #. OPEN "data.txt" FOR OUTPUT AS 1 WRITE #1, 1, "two", 3 ' good data WRITE #1, 2, "eleven" ' missing value causes Input past end of file PRINT #1, 3; ",,"; 10.25 ' unquoted null string PRINT #1, 4; "eight,"; 9.5 ' good data PRINT #1, 5; q$ + "one,"; 9 ' unmatched quote CLOSE 1 ' Read the data back from the file using INPUT #. This results in ' the error "Input past end of data" due to the missing value. OPEN "data.txt" FOR INPUT AS 1 ON ERROR GOTO errorHandler DO UNTIL EOF(1) INPUT #1, a%, b$, c# IF seenError% THEN PRINT "error" ELSE PRINT a%; "'"; b$; "'"; c# END IF LOOP ON ERROR GOTO 0 CLOSE 1 PRINT "==========" ' Read the data again using LINE INPUT #. ' The parseNumber and parseString functions print several error ' messages due to the missing value (parseNumber: null string) and ' other minor problems (unmatched ", unquoted null string). OPEN "data.txt" FOR INPUT AS 1 DO UNTIL EOF(1) LINE INPUT #1, buf$ a% = parseNumber(buf$) b$ = parseString(buf$) c# = parseNumber(buf$) PRINT a%; "'"; b$; "'"; c# LOOP CLOSE 1 END errorHandler: IF ERR = 62 THEN ' input past end of file seenError% = -1 ELSE ERROR ERR END IF RESUME NEXT ' parseNumber ' Purpose ' Retrieve and delete a numeric value from the beginning of an

' input string. The number is terminated by a comma, space, a ' double quote (string value), or the end of the input string. A ' terminating comma is deleted. If no numeric value occurs before ' the terminator, an error message is printed. ' Parameters ' buf$ -- the input string, modified by this function ' Return ' n! -- the number retrieved FUNCTION parseNumber! (buf AS STRING) DIM n AS DOUBLE ' return value DIM s AS STRING ' the value as a string DIM quote AS STRING ' double quote character quote = CHR$(34) ' skip leading spaces DO WHILE LEFT$(buf, 1) = " " buf = MID$(buf, 2) LOOP ' get value into temporary string ' terminated by space, comma, or quote DO UNTIL buf = "" OR INSTR(" ," + quote, LEFT$(buf, 1)) s = s + LEFT$(buf, 1) buf = MID$(buf, 2) LOOP IF s = "" THEN PRINT "parseNumber: null string" n = 0 ELSE n = VAL(s) END IF ' skip trailing spaces and comma DO WHILE LEFT$(buf, 1) = " " buf = MID$(buf, 2) LOOP IF LEFT$(buf, 1) = "," THEN buf = MID$(buf, 2) END IF parseNumber = n END FUNCTION ' parseString ' Purpose ' Retrieve and delete a string value from the beginning of an ' input string. If the value begins with a double quote (") then ' it is terminated by a subsequent double quote, otherwise the ' string is terminated by a comma or the end of the input string. ' A terminating comma is deleted. Error messages are printed if ' the value begins with a double quote but does not end with a ' double quote, or if an unquoted null string value occurs. ' Parameters ' buf$ -- the input string, modified by this function ' Return ' s$ -- the string retrieved FUNCTION parseString$ (buf AS STRING) DIM s AS STRING ' return value DIM quote AS STRING ' double quote character

DIM terminal AS STRING ' close quote or comma quote = CHR$(34) s = "" ' skip leading spaces DO WHILE LEFT$(buf, 1) = " " buf = MID$(buf, 2) LOOP ' determine if quoted or unquoted IF LEFT$(buf, 1) = quote THEN terminal = quote buf = MID$(buf, 2) ELSE terminal = "," END IF ' get value DO UNTIL buf = "" OR LEFT$(buf, 1) = terminal s = s + LEFT$(buf, 1) buf = MID$(buf, 2) LOOP ' check closing quote IF terminal = quote THEN IF buf = "" THEN PRINT "parseString: unmatched " + quote + " on " + s ELSE ' skip closing quote buf = MID$(buf, 2) END IF ' unquoted null string not allowed ELSEIF s = "" THEN PRINT "parseString: unquoted null string" END IF ' skip trailing spaces and comma DO WHILE LEFT$(buf, 1) = " " buf = MID$(buf, 2) LOOP IF LEFT$(buf, 1) = "," THEN buf = MID$(buf, 2) END IF parseString = s END FUNCTION

You might also like