PUT (File I/O)
- Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPutfileio
- Last revised: 2025-06-01
Writes data from a buffer to a file
Syntax
Put #filenum As long, [position As longint], data As Any [, amount As Uinteger]
Put #filenum As long, [position As longint], data As String
Put #filenum As long, [position As longint], data() As AnyUsage
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: i.e. the first record or byte of a file is at position 1.
If position is omitted or zero (0), file writing will start from the current file position.
data
Is the buffer where data is written from. It can be a numeric variable, a string, an array or a user-defined type (including referenced by This). The operation will try to transfer to disk the complete variable, unless amount is given. For a user-defined type instance, the data impacted is only the non-static data members.
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.
When putting Strings, the number of bytes written is the same as the number of bytes in the string data. amount is not allowed.
Note: If you want to write values from a buffer, you should NOT pass a pointer to the buffer; instead you should pass the first variable in the buffer (this can be done by dereferencing the pointer with Operator * (Value of)). If you pass a pointer directly, then Put will put the memory from the pointer variable, not the memory it points to.
amount
Makes Put write to file amount consecutive variables to the file - i.e. it writes ( amount * Sizeof(data) ) bytes of data, starting at data's location in memory, into the file. If amount is omitted it defaults to 1, meaning that Put just writes a single variable.
Return Value
Put() returns a 32 bit Long: 0 on success; nonzero on error. "disk full" is considered as an error, and results in return code 3. An "exact" amount of data written before is not available, and wouldn't be really useful anyway.
Description
Writes binary data from a buffer variable to a file opened in Binary or Random mode.
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.
*Note:
If a real [w/z]string variable is passed to
Put, theamountparameter should be forbidden as it is when passing a string. Do not use. Otherwise, it is dangerously used to multiply the string length to be written to the file, but possibly by overflowing outside the provided [w/z]string buffer.If a dereferenced [w/z]string pointer is passed to
Put, theamountparameter is not taken into account as it is when passing a dereferenced numeric pointer. Do not use. But instead of respecting theamountparameter, the pointed buffer is written to the file up to the zero element (terminal element which is excluded).For finer granularity, any [w/z]string variable can be safely passed to
Putas numeric buffer by providing the first numeric element (an indexed [w/z]string variable, or a dereferenced [w/z]string pointer then indexed) and the number of numeric elements to be processed.*
Note:
Using
Put #is naturally dedicated to Binary/Random Access file mode.It is also allowed in Ouput/Append Access file mode, but this was never well tested and results may vary.