Example of How to Read and Write Visual Basic Arrays to Disk

ID Number: Q77317

1.00

WINDOWS

Summary:

Visual Basic does not provide a command to read or write an entire

array all at once to a disk file. Using Visual Basic 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.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

The ReadArray and WriteArray functions provided below allow you to

read and write a Visual Basic 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). These functions

can work with fixed length strings when the strings are a member of a

user-defined type. These functions cannot accept an array of

fixed-length strings because Visual Basic cannot pass an array of

fixed length strings as a parameter to a Sub or Function. Because Visual

Basic arrays are limited to 64K, the transfer to and from the disk

can be performed in one API function call.

The two functions, ReadArray and WriteArray, require two parameters:

the array to be transferred, and the Visual Basic 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 file number of a file that has already

been opened with the Open statement, and will be used in the Visual

Basic 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 be on just 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 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 = WriteToDisk * -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 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

Additional reference words: 1.00