Jump to main content

##

# 1.2.7.4.19 SYS_FS_FileWrite Function

## C

    size_t SYS_FS_FileWrite
    (
        SYS_FS_HANDLE handle,
        const void *buf,
        size_t nbyte
    );

## Summary

Writes data to the file.

## Description

This function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the file handle.

## Precondition

A valid file handle must be obtained before writing a file.

## Parameters

Param | Description
--- | ---
handle | File handle obtained during file open.
buf | Pointer to buffer from which data is to be written
nbyte | Number of bytes to be written

## Returns

On success - returns the number of bytes written successfully(0 or positive number).

On failure - returns -1. The reason for the failure can be retrieved with SYS_FS_Error or SYS_FS_FileError.

## Example

    SYS_FS_HANDLE fileHandle;
    const char *buf = "Hello World";
    size_t nbytes;
    size_t bytes_written;

    fileHandle = SYS_FS_FileOpen("/mnt/myDrive/FILE.txt", (SYS_FS_FILE_OPEN_WRITE));

    if(fileHandle != SYS_FS_HANDLE_INVALID)
    {
        // File open is successful
    }

    // Write data to the file
    bytes_written = SYS_FS_FileWrite(fileHandle, (const void *)buf, nbytes);

## Remarks

None.