FileSystem.FilePut Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
. F For more information, see FileSystem.
FilePut(Int32, Int64, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, String, Int64, Boolean) |
Writes data from a variable to a disk file. The |
FilePut(Object, Object, Object) |
Obsolete.
Obsolete.
Obsolete.
Writes data from a variable to a disk file. The |
FilePut(Int32, ValueType, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Single, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Int32, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Array, Int64, Boolean, Boolean) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Double, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Decimal, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, DateTime, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Char, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Byte, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Boolean, Int64) |
Writes data from a variable to a disk file. The |
FilePut(Int32, Int16, Int64) |
Writes data from a variable to a disk file. The |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, long Value, long RecordNumber = -1);
static member FilePut : int * int64 * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Long, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Int64
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, string Value, long RecordNumber = -1, bool StringIsFixedLength = false);
static member FilePut : int * string * int64 * bool -> unit
Public Sub FilePut (FileNumber As Integer, Value As String, Optional RecordNumber As Long = -1, Optional StringIsFixedLength As Boolean = false)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- String
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
- StringIsFixedLength
- Boolean
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
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
Caution
This member has been deprecated. Please use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types. http://go.microsoft.com/fwlink/?linkid=14202
Caution
FileSystem.FilePut has been deprecated. Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types.
Caution
Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types
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.
public:
static void FilePut(System::Object ^ FileNumber, System::Object ^ Value, System::Object ^ RecordNumber);
[System.Obsolete("This member has been deprecated. Please use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types. http://go.microsoft.com/fwlink/?linkid=14202")]
public static void FilePut (object FileNumber, object Value, object RecordNumber);
[System.Obsolete("FileSystem.FilePut has been deprecated. Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types.")]
public static void FilePut (object FileNumber, object Value, object RecordNumber);
[System.Obsolete("Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types")]
public static void FilePut (object FileNumber, object Value, object RecordNumber = -1);
[System.Obsolete("This member has been deprecated. Please use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types. http://go.microsoft.com/fwlink/?linkid=14202")]
public static void FilePut (object FileNumber, object Value, object RecordNumber = -1);
[<System.Obsolete("This member has been deprecated. Please use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types. http://go.microsoft.com/fwlink/?linkid=14202")>]
static member FilePut : obj * obj * obj -> unit
[<System.Obsolete("FileSystem.FilePut has been deprecated. Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types.")>]
static member FilePut : obj * obj * obj -> unit
[<System.Obsolete("Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types")>]
static member FilePut : obj * obj * obj -> unit
Public Sub FilePut (FileNumber As Object, Value As Object, RecordNumber As Object)
Public Sub FilePut (FileNumber As Object, Value As Object, Optional RecordNumber As Object = -1)
Parameters
- FileNumber
- Object
Required. Any valid file number.
- Value
- Object
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Object
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
- Attributes
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions (Obsolete) |
---|---|
.NET | (Core 3.0, Core 3.1, 5, 6, 7, 8, 9) |
.NET Framework | (1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1) |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, ValueType Value, long RecordNumber = -1);
static member FilePut : int * ValueType * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As ValueType, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- ValueType
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, float Value, long RecordNumber = -1);
static member FilePut : int * single * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Single, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Single
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, int Value, long RecordNumber = -1);
static member FilePut : int * int * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Integer, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Int32
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, Array Value, long RecordNumber = -1, bool ArrayIsDynamic = false, bool StringIsFixedLength = false);
static member FilePut : int * Array * int64 * bool * bool -> unit
Public Sub FilePut (FileNumber As Integer, Value As Array, Optional RecordNumber As Long = -1, Optional ArrayIsDynamic As Boolean = false, Optional StringIsFixedLength As Boolean = false)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Array
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
- ArrayIsDynamic
- Boolean
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 that describes the length.
- StringIsFixedLength
- Boolean
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
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, double Value, long RecordNumber = -1);
static member FilePut : int * double * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Double, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Double
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, decimal Value, long RecordNumber = -1);
static member FilePut : int * decimal * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Decimal, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Decimal
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, DateTime Value, long RecordNumber = -1);
static member FilePut : int * DateTime * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As DateTime, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- DateTime
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, char Value, long RecordNumber = -1);
static member FilePut : int * char * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Char, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Char
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, byte Value, long RecordNumber = -1);
static member FilePut : int * byte * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Byte, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Byte
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, bool Value, long RecordNumber = -1);
static member FilePut : int * bool * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Boolean, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Boolean
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
- Source:
- FileSystem.vb
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.
public static void FilePut (int FileNumber, short Value, long RecordNumber = -1);
static member FilePut : int * int16 * int64 -> unit
Public Sub FilePut (FileNumber As Integer, Value As Short, Optional RecordNumber As Long = -1)
Parameters
- FileNumber
- Int32
Required. Any valid file number.
- Value
- Int16
Required. Valid variable name that contains data written to disk.
- RecordNumber
- Int64
Optional. Record number (Random
mode files) or byte number (Binary
mode files) at which writing starts.
Exceptions
RecordNumber
< 1 and not equal to -1.
File mode is invalid.
Examples
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
Remarks
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 theFileOpen
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 theRecordLength
clause of theFileOpen
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 theRecordLength
clause in theFileOpen
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 theVarType
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 asVarType(3)
(Integer
) and four bytes that contain the data. The record length specified by theRecordLength
parameter in theFileOpen
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 theVarType(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 theRecordLength
parameter in theFileOpen
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 passTrue
to theStringIsFixedLength
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 toTrue
. 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 theRecordLength
clause in theFileOpen
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 theRecordLength
clause in theFileOpen
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. TheVBFixedString
attribute can be applied to string fields in the structures to indicate the size of the string when written to disk.Note
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 theFileOpen
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 requiresWrite
access from the FileIOPermissionAccess enumeration.
See also
- FileGet
- FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32)
- Seek
- FileGetObject(Int32, Object, Int64)
- VBFixedStringAttribute
- Len(UInt16)
- ArgumentException
- IOException
- Writing to Files in Visual Basic
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: