Input Procedure


Input Procedure

Class

Microsoft.VisualBasic.FileSystem

Syntax

     Input(fileNumber, value) 


fileNumber (required; Integer)

Any valid file number of a file opened with FileOpen


value (required; any)

The destination object into which data from the file will be stored

Description

The Input procedure reads delimited data from a file into a variable. This statement is used to read files created using the Write and WriteLine procedures. Those procedures output data as comma-delimited fields with quotation marks around strings.

Usage at a Glance

  • Data read by Input has usually been written using the Write and WriteLine procedures.

  • Use this statement with files that have been opened in Input or Binary mode only.

  • An error occurs if the data type of value cannot store the data being read by the Input procedure.

  • The Input procedure removes quotation marks that it finds around strings before storing the data in value.

  • After the Input procedure reads value, it advances the file pointer to the next unread variable or, if the file contains no additional delimited data, to the end of the file.

  • An error occurs if the end of the file is reached during the operation.

  • The Input procedure assigns string or numeric data to value without modification. However, other types of data can be modified as shown in the following table:

    Data

    Value assigned to variable

    Delimiting comma or blank line

    "" (empty string)

    #FALSE#

    False

    #trUE#

    TRue

    #yyyy-mm-dd hh:mm:ss#

    Date or time


    #TRUE# and #FALSE# are case-sensitive.

  • Use the EOF function to determine whether the end of the file has been reached.

  • Use the Write and WriteLine procedures to write data to a file, since they delimit data fields correctly. This ensures that the data can be read correctly with the Input procedure.

Example

If the file c:\data.txt contains the following data:

     "one", "two", "three" 

then the following code will print each string on a separate line in the Output window:

     Dim oneValue As String     Dim fileID As Integer = FreeFile(  )     FileOpen(fileID, "c:\data.txt", OpenMode.Input)     Do While Not EOF(fileID)        Input(fileID, oneValue)        Console.WriteLine(oneValue)     Loop     FileClose(fileID) 

Version Differences

  • The Input procedure differs syntactically from its VB 6 counterpart. The VB 6 version supported multiple input values in a single statement, among other changes.

  • In VB 6, if value is numeric and the data read from the file is not numeric, value is initialized to the default value for its type. In .NET, this generates an error.

  • In addition to the standard data types, VB 6 also recognizes Empty, Null, and Error types. In .NET, these are not supported.

  • In Visual Basic 2005, the My.Computer.FileSystem object provides more robust access to file management features.

See Also

Write, WriteLine Procedures




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net