# uwrite command

The uwrite command writes data to a previously opened file on the
 host system.

## Format

```

UWRITE var ON FileHandle THEN statement(s) ELSE statement(s)
```

## Description

A file on the host system is represented as an unformatted string of bytes without internal
 subdividers or markers. READ and WRITE commands provide sequential access to files by advancing
 a pointer within the file. Subsequent READ or WRITE commands advance this internal pointer from
 the current position in the file. The user may specify the exact location within a file to be
 written to by positioning the file pointer at the preferred location before executing the
 command.

 See the [ulseek() function](https://d3codex.com/pickbasic-flashbasic/ulseek-function/) section for more
 information.

 The uwrite command allows the user to write the string value of
 *var* to a file previously described by the *FileHandle*
 variable. The *FileHandle* variable is obtained from the
 uopen command and the ucreate command. The write starts
 at the position indicated by the file pointer. The number of contiguous bytes from the current
 file pointer position to be written is equal to the number of bytes in *var*
 variable.

 Note: These transfers offer optimal performance in 2000-3000 byte buffers.

## Example(s)

```
FILENAME="c:\letter\write.txt"
 UOPEN FIELNAME FOR WRITE TO FILEDES2 ELSE
 PRINT "Unable to open ":FILENAME
 STOP
 END
 TESTDATA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 UWRITE TESTDATA ON FILEDES2 ELSE
 PRINT "Unable to write to ":FILENAME
 END
 UCLOSE FILEDES2 ELSE NULL
```

 The following example displays a file descriptor returned from a uopen
 command being used to write data to a file on the host system. The result is that the alphabet
 is written to the \letter\write2.txt file. If this file had already
 existed, the first 26 characters in the file would be overwritten by this command. Other data in
 the file is not affected.

```
FILENAME="c:\letter\write2.txt"
 UOPEN FILENAME FOR READWRITE TO FILEDES3 ELSE
 PRINT "Unable to open ":FILENAME
 STOP
 END
 TESTDATA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 UWRITE TESTDATA ON FILEDES3 ELSE
 PRINT "Unable to write to ":FILENAME
 END
 UCLOSE FILEDES3 ELSE NULL
```

 The following example uses the file descriptor created from the ucreate
 command to write data to a file on the host system:

```
FILENAME="c:\letter\write3.txt"
 UCREATE FILENAME TO FILEDES4 ELSE
 PRINT "Unable to create and open ":FILENAME
 STOP
 END
 TESTDATA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 UWRITE TESTDATA ON FILEDES4 ELSE
 PRINT "Unable to write to ":FILENAME
 END
 UCLOSE FILEDES4 ELSE NULL
```

## See also

- [O/S interoperability commands and functions](https://d3codex.com/pickbasic-flashbasic/os-interoperability-commands/)
- [uclose command](https://d3codex.com/pickbasic-flashbasic/uclose-command/)
- [ucreate command](https://d3codex.com/pickbasic-flashbasic/ucreate-command/)
- [udelete command](https://d3codex.com/pickbasic-flashbasic/udelete-command/)
- [uerror() function](https://d3codex.com/pickbasic-flashbasic/uerror-function/)
- [uexecute command](https://d3codex.com/pickbasic-flashbasic/uexecute-command/)
- [ulock command](https://d3codex.com/pickbasic-flashbasic/ulock-command/)
- [ulseek() function](https://d3codex.com/pickbasic-flashbasic/ulseek-function/)
- [umessage command](https://d3codex.com/pickbasic-flashbasic/umessage-command/)
- [uopen command](https://d3codex.com/pickbasic-flashbasic/uopen-command/)
- [uread command](https://d3codex.com/pickbasic-flashbasic/uread-command/)
- [ureadline() function](https://d3codex.com/pickbasic-flashbasic/ureadline-function/)
- [usystem() function](https://d3codex.com/pickbasic-flashbasic/usystem-function/)
- [uwaitfor command](https://d3codex.com/pickbasic-flashbasic/uwaitfor-command/)

---
Source: https://d3codex.com/pickbasic-flashbasic/uwrite-command/ - part of the D3Codex reference.
