Legal Data Delimiters when Using INPUT #n Statement (59727)






This article was previously published under Q59727

SUMMARY

This article corrects several documentation errors concerning how to delimit data in a sequential file that is to be read by the INPUT# statement.

The INPUT# statement reads data items from a sequential device or file and assigns them to variables. If the data items in the file are numeric values, they should be separated with a space, carriage return (CR), or comma. Strings should be separated with a carriage return or a comma, or enclosed in double quotation marks. A linefeed (LF) by itself should not be used as a file delimiter in either case.

MORE INFORMATION

The following references incorrectly state that a linefeed may be used as a delimiter between data items in a file. These references also omit the fact that string data may be delimited by a comma.

  • Page 304 of the "Microsoft QuickBasic Compiler" manual for versions 2.0, 2.01, and 3.0
  • Page 225 of the "Microsoft QuickBasic 4.0: Basic Language Reference" manual for QuickBasic versions 4.0 and 4.0b
  • Page 225 of the "Microsoft Basic Compiler 6.0: Basic Language Reference" manual for versions 6.0 and 6.0b
  • Page 167 of the "Microsoft Basic 7.0: Language Reference" manual for Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1
  • Under the INPUT# statement in the QB Advisor online Help system for QuickBasic version 4.5
In addition, all the above references (except page 167) incorrectly state that string data items may be delimited with spaces. Only numeric data items may be delimited with spaces. String data items must be delimited by a comma or a carriage return, or enclosed in double quotation marks.

If you need some other character (for example, a linefeed by itself) to act as a delimiter, then a file may be read in BINARY mode. When a file is opened in BINARY mode, the data is not interpreted and the program must be written to interpret or "filter" each character as needed.

The following is a description of how the INPUT# statement handles a linefeed, carriage return, space, and comma if one of these characters is used as a delimiter between data items. Also, the program below exhibits the behavior of these characters when used as delimiters.

Numeric Input Syntax: INPUT #n, <numeric variable>

This reads a linefeed in as a numeric value instead of a delimiter. Each time a linefeed is encountered, the linefeed character is treated as a data item and a value of 0 is returned for the input.

A carriage return, space, or comma functions correctly as a delimiter for numeric input.

String Input Syntax: INPUT #n, <string variable>

This ignores the linefeed character completely. If two data items are separated by a linefeed and read in as strings, the two data items are read in as one string that is a concatenation of the two data items.

A space between two data items causes the two data items to be read in as one string, and the space is an actual character in that string.

A carriage return or comma functions correctly as a delimiter. If a comma appears between a pair of double quotation marks, the comma is treated as part of the string. A carriage return always acts as a delimiter, terminating any string delimited by a beginning double quotation mark.

Sample Code

CLS
INPUT "Enter 'q' to quit.  Enter 'c' to continue==> ", start$

WHILE start$ <> "q"
 CLS

 OPEN "numeric.dat" FOR OUTPUT AS #1
 OPEN "string.dat" FOR OUTPUT AS #2

 PRINT "Input the ASCII code for the delimiter you wish to attempt:"
 PRINT "   10 = line feed"
 PRINT "   13 = carriage return"
 PRINT "   32 = space"
 INPUT "   44 = comma =============>", delimit%

 num1% = 5: num2% = 10        'define data to be put in numeric file
 str1$ = "hi": str2$ = "mom"  'define data to be put in string file

 PRINT #1, num1%; CHR$(delimit%); num2%  'write data separated by
 PRINT #2, str1$; CHR$(delimit%); str2$  'chosen delimiter to file

 CLOSE

 OPEN "numeric.dat" FOR INPUT AS #1
 OPEN "string.dat" FOR INPUT AS #2

 PRINT
 count% = 0
 PRINT "This file contains the numeric values 5 and 10."
 PRINT "==============================================="
 DO UNTIL EOF(1)
     count% = count% + 1
     INPUT #1, inp1%
     PRINT "Value read in after"; count%; "input(s):"; inp1%
 LOOP

 PRINT
 count% = 0
 PRINT "This file contains the string values 'hi' and 'mom'."
 PRINT "===================================================="
 DO UNTIL EOF(2)
     count% = count% + 1
     INPUT #2, inp2$
     PRINT "Value read in after"; count%; "input(s): "; inp2$
 LOOP

 CLOSE

 PRINT : PRINT
 INPUT "Press 'q' to quit.  Press 'c' to continue==> ", start$
WEND
END
				

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB59727