Put (File I/O)
 
Writes data from a buffer to a file

Syntax

Put #filenum As Integer, [position As LongInt], data As Any [, amount As Integer]
Put #filenum As Integer, [position As LongInt], data() As Any

Usage

Put #filenum, position, data [, amount]
varres = Put (#filenum, position, data [, amount])

Parameters

filenum
The value passed to Open when the file was opened.
position
Is the position where Put must start in the file. If the file was opened For Random, the position is in records, else it is given in bytes. If omitted, writing starts at the present file pointer position. The position is 1 based: the first record or byte of a file is at position 1.
data
Is the buffer where data is written from. It can be a numeric variable, a string, an array or a user-defined type. The operation will try to transfer to disk the complete variable, unless amount is given. When putting arrays, data should be followed by an empty pair of brackets: "()". Put will write all of the data in the array. amount is not allowed.
amount
Makes Put write to file amount * SizeOf(buffer_datatype) bytes of data. If amount is omitted, Put just writes a single variable.

Return Value

0 on success; nonzero on error.

Description

Writes binary data from a buffer variable to a file.

Put can be used as a function, and will return 0 on success or an error code on failure.

For files opened in Random mode, the size in bytes of the data to write must match the specified record size.

Example

' Create a integer variable
Dim buffer As Integer, f As Integer

' Find the first free file file number.
f = FreeFile

' Open the file "file.ext" for binary usage, using the file number "f".
Open "file.ext" For Binary As #f
  buffer=10
  ' Write 4 bytes from the buffer into the file, using file number "f"
  ' starting at the beginning of the file (1).
  Put #f, 1, buffer

' Close the file.  
Close #f

' End the program.
End


' Create an integer array
Dim buffer(1 To 10) As Integer
Dim i As Integer
For i = 1 To 10
    buffer(i) = i
Next

' Find the first free file file number.
Dim f As Integer
f = FreeFile

' Open the file "file.ext" for binary usage, using the file number "f".
Open "file.ext" For Binary As #f
' Write the array into the file, using file number "f"
' starting at the beginning of the file (1).
Put #f, 1, buffer()

' Close the file.
Close #f

' End the program.
End


Example

Dim As Byte Ptr lpBuffer
Dim As Integer hFile, Counter, Size

Size=256

lpBuffer=Allocate(Size)
For Counter=0 To Size-1
  lpBuffer[Counter]=(Counter And &HFF)
Next

' Get free file file number.
hFile = FreeFile()

' Open the file "test.bin" in binary writing mode.
Open "test.bin" For Binary Access Write As #hFile
  ' Write 256 bytes from the memory pointened by lpBuffer.
  Put #hFile, , lpBuffer[0],Size
' Close the file.  
Close #hFile
' Free the allocated memory.
Deallocate lpBuffer
' End the program.
End

Differences from QB

  • PUT can write full arrays as in VB or, alternatively, write a multiple of the data size from buffer's memory location.
  • PUT can now be used as a function.

See also