Export (0) Print
Expand All

FileSystem.FilePut Method (Int32, Int64, Int64)

Writes data from a variable to a disk file. The My feature gives you better productivity and performance in file I/O operations than FilePut. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static void FilePut(
	int FileNumber,
	long Value,
	long RecordNumber
)

Parameters

FileNumber
Type: System.Int32

Required. Any valid file number.

Value
Type: System.Int64

Required. Valid variable name that contains data written to disk.

RecordNumber
Type: System.Int64

Optional. Record number (Random mode files) or byte number (Binary mode files) at which writing starts.

ExceptionCondition
ArgumentException

RecordNumber < 1 and not equal to -1.

IOException

FileNumber does not exist.

IOException

File mode is invalid.

FilePut is valid only in Random and Binary mode.

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

The first record or byte in a file is at position 1, the second record or byte is 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 have to do the same with FileGet, and you have to make sure that the string is initialized to the length expected.

Random Mode

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 start of the next record is padded with the existing contents of the file buffer. Because the amount of padding data cannot 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 that contains 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 that contains a numeric type, FilePut writes two bytes identifying the VarType of the object and then writes the variable. For example, when writing an object that contains an integer, FilePut writes six bytes: two bytes that identify the object as VarType(3) (Integer) and four bytes that contain 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 that contains 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 want to put a string without the descriptor, 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, you have a choice as to whether or not to write a descriptor for the size and dimensions of the array. Visual Basic 6.0 and earlier versions write the file descriptor for a dynamic array but not for a fixed-size array. Visual Basic 2005 defaults to not writing the descriptor. To write the descriptor, set the ArrayIsDynamic parameter to True. When writing the array, you have to match the way the array will be read; if it will be read with the descriptor, you have 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: (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 218 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.

    NoteNote

    String fields that have more bytes than specified by the VBFixedString attribute are truncated when written to disk,

Binary Mode

For files opened in Binary mode, most of the Random mode rules apply, with some exceptions. The following rules for files opened in Binary mode differ from the rules for Random mode:

  • The RecordLength clause in the FileOpen function has no effect. FilePut writes all variables to disk contiguously, that is, without 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 by using the FilePut function requires Write access from the FileIOPermissionAccess enumeration.

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 PatientRecord As Person
    Dim recordNumber As Integer 
    '    Open file for random access.
    FileOpen(1, "C:\TESTFILE.txt", OpenMode.Binary)
    ' Loop 5 times. 
    For recordNumber = 1 To 5
        ' Define ID.
        PatientRecord.ID = recordNumber
        ' Create a string.
        PatientRecord.Name = "Name " & recordNumber
        ' Write record to file.
        FilePut(1, PatientRecord)
    Next recordNumber
    FileClose(1)
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft