How to Read and Write Visual Basic Arrays to Disk
ID: Q77317
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0
-
Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARY
Microsoft Visual Basic for Windows does not provide a command to
read or write an entire array all at once to a disk file. Using Visual
Basic for Windows alone, you must transfer each element of the array
to the disk. However, using two Windows API functions, _lread and
_lwrite, you can save an entire array to disk in one statement when
the array is less then 64K.
MORE INFORMATION
The ReadArray and WriteArray functions provided below allow you to
read and write a Visual Basic for Windows array to or from a disk file.
These functions will work with arrays of Integers, Longs, Singles,
Doubles, Currency, and user-defined types, but not with
variable-length strings (as an array or as a member of a user-defined
type) or variants. These functions can work with fixed length strings
when the strings are a member of a user-defined type. Arrays greater
than 64K are supported in Visual Basic versions 2.0 and later for
Windows, however the _lread and _lwrite functions can only handle
arrays up to 64K. Arrays greater than 64K can be written to disk using
the standard I/O statements built into Visual Basic for Windows.
The two functions, ReadArray and WriteArray, require two parameters:
the array to be transferred, and the Visual Basic for Windows file
number to be written to or read from. The functions also return the
number of bytes transferred, or -1 when an error occurs with the API
function. The file number is the Visual Basic for Windows file number
of a file that has already been opened with the Open statement, and
will be used in the Visual Basic for Windows Close statement.
The following function examples use a user-defined type named "Mytype".
An example of this type is as follows:
Type MyType
Field1 As String * 10
Field2 As Integer
Field3 As Long
Field4 As Single
Field5 As Double
Field6 As Currency
End Type
Declarations of API Functions
DefInt A-Z
' Each Declare statement must appear on one line:
Declare Function fWrite Lib "kernel" Alias "_lwrite" (ByVal hFile,
lpBuff As Any, ByVal wBytes)
Declare Function fRead Lib "kernel" Alias "_lread" (ByVal hFile,
lpBuff As Any, ByVal wBytes)
Function: ReadArray
Function ReadArray (An_Array() As MyType, VBFileNumber As Integer) As Long
Dim ApiErr As Integer
Dim ArraySize As Long
Dim DOSFileHandle As Integer
Dim ReadFromDisk As Integer
ArraySize = Abs(UBound(An_Array) - LBound(An_Array)) + 1
ArraySize = ArraySize * Len(An_Array(LBound(An_Array)))
If ArraySize > 32767 Then
ReadFromDisk = ArraySize - 2 ^ 15
ReadFromDisk = ReadFromDisk * -1
Else
ReadFromDisk = ArraySize
End If
DOSFileHandle = FileAttr(VBFileNumber, 2)
ApiErr=fRead(DOSFileHandle,An_Array(LBound(An_Array)),ReadFromDisk)
ReadArray = ApiErr
End Function
Function: WriteArray
Function WriteArray (An_Array() As MyType, VBFileNumber As Integer) As Long
Dim ApiErr As Integer
Dim ArraySize As Long
Dim DOSFileHandle As Integer
Dim WriteToDisk As Integer
ArraySize = UBound(An_Array) - LBound(An_Array) + 1
ArraySize = ArraySize * Len(An_Array(LBound(An_Array)))
If ArraySize > 32767 Then
WriteToDisk = ArraySize - 2 ^ 15
WriteToDisk = WriteToDisk * -1
Else
WriteToDisk = ArraySize
End If
DOSFileHandle = FileAttr(VBFileNumber, 2)
ApiErr=fWrite(DOSFileHandle,An_Array(LBound(An_Array)),WriteToDisk)
WriteArray = ApiErr
End Function
The following are the function header changes to allow the ReadArray
and WriteArray functions to work with different data types (Integer,
Long, Single, Double, Currency, and user-defined type). Each Function
statement must be on a single line:
Function ReadArray (An_Array() As Integer, VBFileNumber As Integer)
As Long
Function WriteArray (An_Array() As Integer, VBFileNumber As Integer)
As Long
Function ReadArray (An_Array() As Long, VBFileNumber As Integer) As
Long
Function WriteArray (An_Array() As Long, VBFileNumber As Integer) As
Long
Function ReadArray (An_Array() As Single, VBFileNumber As Integer) As
Long
Function WriteArray (An_Array() As Single, VBFileNumber As Integer) As
Long
Function ReadArray (An_Array() As Double, VBFileNumber As Integer) As
Long
Function WriteArray (An_Array() As Double, VBFileNumber As Integer) As
Long
Function ReadArray (An_Array() As Currency, VBFileNumber As Integer)
As Long
Function WriteArray (An_Array() As Currency, VBFileNumber As Integer)
As Long
Keywords : kbcode
Version : 1.0 2.0 3.0
Platform : WINDOWS
Issue type : kbhowto