Visual Basic Language Reference  

FilePut Function

Writes data from a variable to a disk file.

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Short, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Integer, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Single, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Double, _
   RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Decimal, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Byte, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Boolean, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As Date, _
   Optional RecordNumber As Integer = -1 _
)

-or-

Public Overloads Sub FilePut( _
   FileNumber As Integer, _
   Value As System.Array, _
   Optional RecordNumber As Integer = -1, _
   Optional ArrayIsDynamic As Boolean = False _
)

-or-

Public Overloads Sub FilePut (
   FileNumber As Integer, 
   Value As String, _
   Optional RecordNumber As Integer = -1, 
   Optional StringIsFixedLength As Boolean = False
)

Parameters

FileNumber
Required. Any valid file number.
Value
Required. Valid variable name containing data written to disk.
RecordNumber
Optional. Record number (Random mode files) or byte number (Binary mode files) at which writing begins.
ArrayIsDynamic
Optional. Applies only when writing an array. Specifies whether the array is to be treated as dynamic, and whether to write an array descriptor for the string describing the length.
StringIsFixedLength
Optional. Applies only when writing a string. Specifies whether to write a two-byte string length descriptor for the string to the file. The default is False.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 63 RecordNumber < 1 and not equal to -1.
IOException 52 FileNumber does not exist.
IOException 54 File mode is invalid.

Remarks

FilePut is only valid in Random and Binary mode.

Data written with FilePut is usually read from a file with FileGet.

The first record or byte in a file is at position 1, the second record or byte at position 2, and so on. If you omit RecordNumber, the next record or byte after the last FileGet or FilePut function or pointed to by the last Seek function is written.

The StringIsFixedLength argument controls whether the function interprets strings as variable or fixed length. FilePut does not write the length descriptor when the argument is True. If you use StringIsFixedLength = True with FilePut, you will need to do the same with FileGet, and also make sure the string is initialized to the length expected.

For files opened in Random mode, the following rules apply:

For files opened in Binary mode, all of the Random mode rules apply, except:

Example

This example uses the FilePut function to write data to a file. Five records of the structure Person are written to the file.

Structure Person 
   Public ID As Integer
   Public Name As String
End Structure

Sub WriteData()
   Dim MyRecord As Person
   Dim recordNumber As Integer    
'    Open file for random access.
   FileOpen(1, "C:\TESTFILE.txt", OpenMode.Binary)
   For recordNumber = 1 To 5   ' Loop 5 times.
      MyRecord.ID = recordNumber   ' Define ID.
      MyRecord.Name = "My Name" & recordNumber   ' Create a string.
      FilePut(1, MyRecord)   ' Write record to file.
   Next recordNumber
   FileClose(1)
End Sub

See Also

FileGet Function | FileOpen Function | Seek Function | File Access with Visual Basic Run-Time Functions | FileGetObject Function | ArgumentException | IOException