Reading A File
Reading a file means extracting the characters that
are in the file and loading the data into the memory
buffer. To read a file, you must
- Determine how many characters to read
- Provide variables to receive the data
VB provides several functions and statements to read
a sequential file:
- Input
- Input #
- Line Input #
Input function
this function returns a string containing characters
from a file opened in Input or Binary mode.
Input(number, [#]filenumber)
number: specifying the number of characters to return
filenumber: Any valid file number.
The following code segment reads 10 characters into
the string variable LastName:
LastName = Input(10, #1)
Input # Function
The Input # statement reads data from an open sequential
file and assigns the data to variables.
Input #filenumber, varlist
Assume that the file contains the following: Amit,
Sumit. Using the following statement the data can
be read into local variables Name1 and Name2:
Input #1, Name1, Name2
Line Input # Statement
This function reads a single line from an open sequential
file and assigns it to a string variable.
Line Input #filenumber, varname
For example:
Line Input #1, OneLine
|