## pf_write

The pf_write function writes data to the file.

    FRESULT pf_write (
      const void* buff, /* [IN]  Pointer to the data to be written */
      UINT btw,         /* [IN]  Number of bytes to write */
      UINT* bw          /* [OUT] Pointer to the variable to return number of bytes written */
    );

#### Parameters

buff
  Pointer to the data to be wtitten. A NULL specifies to finalize the current write operation.
btw
  Number of bytes to write.
bw
  Pointer to the variable to return number of bytes read.

#### Return Values

FR_OK (0)
  The function succeeded.
FR_DISK_ERR
  The function failed due to a hard error in the disk function, write protected, a wrong FAT structure or an internal error.
FR_NOT_OPENED
  The file has not been opened.
FR_NOT_ENABLED
  The volume has not been mounted.

#### Description

The write function has some restrictions listed below:

- Cannot create file. Only existing file can be written.
- Cannot expand file size.
- Cannot update time stamp of the file.
- Write operation can start/stop on the sector boundary.
- Read-only attribute of the file cannot block write operation.

File write operation must be done in following sequence.

1. `pf_lseek(ofs);` read/write pointer must be moved to sector bundary prior to initiate write operation or it will be rounded-down to the sector boundary.
2. `pf_write(buff, btw, &bw);` Initiate write operation. Write first data to the file.
3. `pf_write(buff, btw, &bw);` Write next data. Any other file function cannot be used while a write operation is in progress.
4. `pf_write(0, 0, &bw);` Finalize the write operation. If read/write pointer is not on the sector boundary, left bytes in the sector will be filled with zero.

The read/write pointer in the file system object advances in number of bytes written. After the function succeeded, `*bw` should be checked to detect end of file. In case of `*bw < btw`, it means the read/write pointer reached end of file during the write operation. Once a write operation is initiated, it must be finalized or the written data can be lost.

#### QuickInfo

Available when `_USE_WRITE == 1`.

#### References

`pf_open, FATFS`

Return