FilePut, FilePutObject Procedures

   
FilePut, FilePutObject Procedures

Class

Microsoft.VisualBasic.FileSystem

Syntax

 FilePut(f   ilenumber, value   , [   recordnumber   ]) FilePutObject(   filenumber, value   , [   recordnumber   ]) 
filenumber (required; Integer)

Any valid file number

value (required; any (see the first item in Section )

The name of the variable containing the data to be written to the file

recordnumber (optional; Integer)

Record number (for random access) or byte number (for binary access) at which to begin the write operation

Description

Writes data from a program variable to a disk file

Rules at a Glance

  • The value argument of the FilePut procedure can be any data type except Object. The value argument of the FilePutObject procedure must be of type Object.

  • If filenumber is opened in random access mode, recordnumber refers to the record number; if the file is opened in binary access mode, recordnumber refers to a byte number.

  • Both bytes and records in a file are numbered starting with 1.

  • If recordnumber is omitted, the next byte or record to be written will be placed at the position immediately after the position pointed to by the last FileGet or FilePut procedure, or by the last Seek function.

  • If you have opened the file in Random mode, it is important to ensure that the record length specified in the recordNumber 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 recordNumber argument, the space up to the end of the record will be padded with the current contents of the file buffer whatever that may be. If, on the other hand, the actual data length is more than that specified, an error occurs.

  • The FilePut procedure cannot be used to write objects to disk. The FilePutObject procedure is used for this purpose.

  • If you open the file in Binary mode, the RecordNumber argument 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.

Example

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

 Dim fr As Integer = FreeFile(  ) Dim sChar As Char Dim i As Integer FileOpen(fr, "c:\data2.txt", OpenMode.Binary) For i = Asc("A") To Asc("Z")     sChar = Chr(i)     FilePut(fr, sChar)  Next FileClose(fr) 

Programming Tips and Gotchas

  • Because of the structured format of data written with the FilePut procedure, it is customary to read the data back from the file using the FileGet procedure.

  • The FilePutObject procedure can be used to write data of type Object whose subtype is one of the standard datatypes (Boolean, Byte, Char, etc.). It cannot be used to write object data defined by the Class...End Class construct (including classes residing in .NET libraries), nor can it be used to write data from COM objects to disk. The following is a rewritten version of the example code that uses FilePutObject :

     Dim fr As Integer = FreeFile(  ) Dim oChar As Object Dim i As Integer FileOpen(fr, "c:\data2.txt", OpenMode.Binary) For i = Asc("A") To Asc("D")     oChar = Chr(i)     FilePutObject(fr, oChar) Next FileClose(fr) 
  • If you use the FilePut procedure to write data, you can use the FileGet procedure to read it. Similarly, if you use the FilePutObject procedure, you should should the FileGetObject procedure.

VB.NET/VB 6 Differences

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

See Also

FileClose Procedure, FileGet, FileGetObject Procedures, FileOpen Procedure

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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