This documentation is archived and is not being maintained.

FileSystem.FilePut Method (Int32, DateTime, Int64)

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

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

public static void FilePut (
	int FileNumber,
	DateTime Value,
	[OptionalAttribute] long RecordNumber
)
public static void FilePut (
	int FileNumber, 
	DateTime Value, 
	/** @attribute OptionalAttribute() */ long RecordNumber
)
public static function FilePut (
	FileNumber : int, 
	Value : DateTime, 
	RecordNumber : long
)

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.

For more detailed information, see the Visual Basic topic FilePut Function.

FilePut is valid only 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 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 need to do the same with FileGet, and you need to make sure 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 beginning 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 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.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 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: (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.

    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 a few 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, 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.

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: