Input# (statement)

Syntax

Input [#]filenumber%,variable[,variable]...

Description

Reads data from the file referenced by filenumber into the given variables.

Comments

Each variable must be type-matched to the data in the file. For example, a String variable must be matched to a string in the file.

The following parsing rules are observed while reading each variable in the variable list:

  1. Leading white space is ignored (spaces and tabs).

  2.  When reading String variables, if the first character on the line is a quotation mark, then characters are read up to the next quotation mark or the end of the line, whichever comes first. Blank lines are read as empty strings. If the first character read is not a quotation mark, then characters are read up to the first comma or the end of the line, whichever comes first. String delimiters (quotes, comma, end-of-line) are not included in the returned string.

  3. When reading numeric variables, scanning of the number stops when the first nonnumber character (such as a comma, a letter, or any other unexpected character) is encountered. Numeric errors are ignored while reading numbers from a file. The resultant number is automatically converted to the same type as the variable into which the value will be placed. If there is an error in conversion, then 0 is stored into the variable.

  octaldigits[!|#|%|&|@]

After reading the number, input is skipped up to the next delimiter—a comma, an end-of-line, or an end-of-file.

Numbers must adhere to any of the following syntaxes:

  [-|+]digits[.digits][E[-|+]digits][!|#|%|&|@]

  &Hhexdigits[!|#|%|&]

  &[O]

  1. When reading Boolean variables, the first character must be #; otherwise, a runtime error occurs. If the first character is #, then input is scanned up to the next delimiter (a comma, an end-of-line, or an end-of-file). If the input matches #FALSE#, then FALSE is stored in the Boolean; otherwise TRUE is stored.

  2. When reading Date variables, the first character must be #; otherwise, a runtime error occurs. If the first character is #, then the input is scanned up to the next delimiter (a comma, an end-of-line, or an end-of-file). If the input ends in a # and the text between the #'s can be correctly interpreted as a date, then the date is stored; otherwise, December 31, 1899, is stored.

Normally, dates that follow the universal date format are input from sequential files. These dates use this syntax:

    #YYYY-MM-DD HH:MM:SS#

where YYYY is a year between 100 and 9999, MM is a month between 1 and 12, DD is a day between 1 and 31, HH is an hour between 0 and 23, MM is a minute between 0 and 59, and SS is a second between 0 and 59.

  1. When reading Variant variables, if the data begins with a quotation mark, then a string is read consisting of the characters between the opening quotation mark and the closing quotation mark, end-of-line, or end-of-file.

If the input does not begin with a quotation mark, then input is scanned up to the next comma, end-of-line, or end-of-file and a determination is made as to what data is being represented. If the data cannot be represented as a number, Date, Error, Boolean, or Null, then it is read as a string.

The following table describes how special data is interpreted as variants:

 

 

Blank Line

Read as an Empty variant

 

 

#NULL#

Read as a Null variant.

 

 

#TRUE#

Read as a Boolean variant.

 

 

#FALSE#

Read as a Boolean variant.

 

 

#ERROR code#

Read as a user-defined error.

 

 

#date#

Read as a Date variant.

 

 

"text"

Read as a String variant.

 

If an error occurs in interpretation of the data as a particular type, then that data is read as a String variant.

When reading numbers into variants, the optional type-declaration character determines the VarType of the resulting variant. If no type-declaration character is specified, then The Basic Control Engine will read the number according to the following rules:

Rule 1: If the number contains a decimal point or an exponent, then the number is read as Currency. If there is an error converting to Currency, then the number is treated as a Double.

Rule 2: If the number does not contain a decimal point or an exponent, then the number is stored in the smallest of the following data types that most accurately represents that value: Integer, Long, Currency, Double.

  1. End-of-line is interpreted as either a single line feed, a single carriage return, or a carriage-return/line-feed pair. Thus, text files from any platform can be interpreted using this command.

The filenumber parameter is a number that is used by The Basic Control Engine to refer to the open file, the number passed to the Open statement.

The filenumber must reference a file opened in Input mode. It is good practice to use the Write statement to write date elements to files read with the Input statement to ensure that the variable list is consistent between the input and output routines.

Example

This example creates a file called test.dat and writes a series of variables into it. Then the variables are read using the Input# function.

Const crlf = Chr$(13) + Chr$(10)

Sub Main()
  Open "test.dat" For Output As #1
  Write #1,2112,"David","McCue","123-45-6789"
  Close

  Open "test.dat" For Input As #1
  Input #1,x%,s1$,s2$,s3$
  msg1 = "Employee #" & x% & " Personal Information" & crlf & crlf
  msg1 = msg1 & "First Name: " & s1$ & crlf
  msg1 = msg1 & "Last Name: "& s2$ & crlf
  msg1 = msg1 & "Social Security Number: " & s3$
  MsgBox msg1
  Close

  Kill "test.dat"
End Sub

See Also

Open (statement); Get (statement); Line Input# (statement); Input, Input$ (functions).

More information

I