FilePut, FilePutObject Procedures


FilePut, FilePutObject Procedures

Class

Microsoft.VisualBasic.FileSystem

Syntax

     FilePut(fileNumber, value[, recordNumber[, dataFlag]])     FilePutObject(fileNumber, value[, recordNumber[, dataFlag]]) 


fileNumber (required; Integer)

Any valid file number of a file opened with FileOpen.


value (required; multiple data types)

Variable or data to be written to the file. May be one of the following data types: Object, Short, Integer, Single, Double, Decimal, Byte, Boolean, Date, System.Array, or String.


recordNumber (optional; Integer)

The 1-based location at which writing begins, either a record number (for Random mode) or a byte number (for Binary mode). If omitted, it defaults to -1, which indicates the next available record in the file should be used.


dataFlag (optional; Boolean)

New in 2005. This flag is only used when value is of type System.Array or String. For System.Array data, the flag indicates whether the array is dynamic (true) or not (False). For strings, the flag indicates whether the string is fixed in size (TRue) or not (False). If omitted, this field defaults to False.

Description

The FilePut and FilePutObject procedures write data to an open file. For files open in Random mode, the data is written as a record. For Binary files, the data is written as a stream of bytes. FilePut and FilePutObject are identical in functionality, but using FilePutObject may reduce compile-time data conversion issues when working with Object data values.

Usage at a Glance

  • If you have opened a file in Random mode, it is important to ensure that the record length specified in the recordLength argument of the FileOpen procedure matches the actual length of the data being written. If the length of the data being written is less than that specified by the recordLength argument, the space up to the end of the record will be padded. If the actual data length is more than that specified, an error occurs.

  • If you open the file in Binary mode, the recordLength argument of the FileOpen procedure has no effect. When you use FilePut to write data to the disk, the data is written contiguously, and no padding is placed between records.

  • Records written with FilePut and FilePutObject are normally read using FileGet and FileGetObject.

Example

The following code writes the letters A-Z to a file:

     Dim oneChar As Char     Dim counter As Integer     Dim fileID As Integer = FreeFile(  )     FileOpen(fileID, "c:\data.txt", OpenMode.Binary)     For counter = Asc("A") To Asc("Z")        oneChar = Chr(counter)        FilePut(fileID, oneChar)     Next counter     FileClose(fileID) 

Version Differences

  • The FilePut and FilePutObject procedures are new to .NET. They are almost direct replacements for the VB 6 Put statement.

  • The dataFlag argument in both the FilePut and the FilePutObject functions is new with Visual Basic 2005.

  • In Visual Basic 2005, the My.Computer.FileSystem object provides more robust access to file-management features.

See Also

FileClose Procedure, FileGet, FileGetObject Procedures, FileOpen Procedure




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net