Microsoft KB Archive/59727

= Legal Data Delimiters when Using INPUT #n Statement =

Article ID: 59727

Article Last Modified on 11/21/2006



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 &quot;Microsoft QuickBasic Compiler&quot; manual for versions 2.0, 2.01, and 3.0
 * Page 225 of the &quot;Microsoft QuickBasic 4.0: Basic Language Reference&quot; manual for QuickBasic versions 4.0 and 4.0b
 * Page 225 of the &quot;Microsoft Basic Compiler 6.0: Basic Language Reference&quot; manual for versions 6.0 and 6.0b
 * Page 167 of the &quot;Microsoft Basic 7.0: Language Reference&quot; 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 &quot;filter&quot; 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,
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,
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 &quot;Enter 'q' to quit. Enter 'c' to continue==> &quot;, start$

WHILE start$ <> &quot;q&quot; CLS

OPEN &quot;numeric.dat&quot; FOR OUTPUT AS #1 OPEN &quot;string.dat&quot; FOR OUTPUT AS #2

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

num1% = 5: num2% = 10       'define data to be put in numeric file str1$ = &quot;hi&quot;: str2$ = &quot;mom&quot; '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 &quot;numeric.dat&quot; FOR INPUT AS #1 OPEN &quot;string.dat&quot; FOR INPUT AS #2

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

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

CLOSE

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

Additional query words: QuickBas BasicCom 2.00 3.00 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10 quotes

Keywords: KB59727

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.