Petit Fat is extremely limited for file write.
The write function has some restrictions listed below:
Cannot create file. Only existing file can be written.
Cannot expand file size.
Write operation must start/stop on the sector boundary.
File write operation must be done in following sequence.
pf_lseek(ofs); read/write pointer must be moved to sector boundary prior to initiate write operation or it will be rounded-down to the sector boundary.
pf_write(buff, btw, &bw); Initiate write operation. Write first data to the file.
pf_write(buff, btw, &bw); Write next data. Any other file function cannot be used while a write operation is in progress.
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 increases in number of bytes written. After the function succeeded, *BytesWritten should be checked to detect end of file. In case of *BytesWritten < ByteToWrite, 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.