Export (0) Print
Expand All

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:

  • If the length of the data being written is less than the length specified in the RecordLength clause of the FileOpen function, FilePut writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data can't be determined with any certainty, it is generally a good idea to have the record length match the length of the data being written. If the length of the data being written is greater than the length specified in the RecordLength clause of the FileOpen function, an exception will be thrown.
  • If the variable being written is a string, FilePut writes a two-byte descriptor containing the string length, and then writes the data that goes into the variable. Therefore, the record length specified by the RecordLength clause in the FileOpen function must be at least two bytes greater than the actual length of the string.
  • If the variable being written is an object containing a numeric type, FilePut writes two bytes identifying the VarType of the object and then writes the variable. For example, when writing an object containing an integer, FilePut writes six bytes: two bytes identifying the object as VarType(3) (Integer) and four bytes containing the data. The record length specified by the RecordLength parameter in the FileOpen function must be at least two bytes greater than the actual number of bytes required to store the variable.
  • If the variable being written is an object containing a string, FilePut writes a two byte descriptor identifying the VarType(8) of the object, a two-byte descriptor indicating the length of the string, and then writes the string data. The record length specified by the RecordLength parameter in the FileOpen function must be at least four bytes greater than the actual length of the string. If you wish to put a string without the descriptor, then you should pass True to the StringIsFixedLength parameter, and the string you read into should be the correct length.
  • If the variable being written is an array, then you have a choice as to whether or not to write a descriptor for the size and dimensions of the array. Visual Basic 6 and earlier versions would write the file descriptor for a dynamic array, but not for a fixed-size array. Visual Basic .NET defaults to not writing the descriptor. To write the descriptor, set the ArrayIsDynamic parameter to True. When writing the array, you need to match the way the array will be read; if it will be read with the descriptor, then you need to write the descriptor. The descriptor specifies the rank of the array, the size, and the lower bounds for each rank. Its length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the RecordLength clause in the FileOpen function must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. For example, the following array declaration requires 118 bytes when the array is written to disk.
    Dim MyArray(4,9) As Integer
    
  • If the variable being written is any other type of variable (not a variable-length string or an object), FilePut writes only the variable data. The record length specified by the RecordLength clause in the FileOpen function must be greater than or equal to the length of the data being written.
  • FilePut writes elements of structures as if each were written individually, except there is no padding between elements. The VBFixedString attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.

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

  • The RecordLength clause in the FileOpen function has no effect. FilePut writes all variables to disk contiguously; that is, with no padding between records.
  • For any array other than an array in a structure, FilePut writes only the data. No descriptor is written.
  • FilePut writes variable-length strings that are not elements of structures without the two-byte length descriptor. The number of bytes written equals the number of characters in the string. For example, the following statements write 11 bytes to file number 1:
    Dim hellow As String = "Hello World"
    FilePut(1,hellow)
    
  • Writing to a file with the FilePut function requires Write access from the FileIOPermissionAccess enumeration. For more information, see FileIOPermissionAccess Enumeration.

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

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

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

Show:
© 2014 Microsoft