### _write -- Writes from Buffer to File

Format

>     #include <io.h>
>     int _write(int handle, const void *buffer, unsigned int count);

Language Level: XPG4, Extension
_write writes count bytes from buffer into the file associated with handle. The write operation begins at the current position of the file pointer associated with the given file. If the file is open for appending, the operation begins at the end of the file. After the write operation, the file pointer is increased by the number of bytes actually written.

If the given file was opened in text mode, each line feed character is replaced with a carriage return/line feed pair in the output. The replacement does not affect the return value.

Return Value
_write returns the number of bytes moved from the buffer to the file. The return value may be positive but less than count. A return value of -1 indicates an error, and errno is set to one of the following values:

Value | Meaning
EBADF | The file handle is not valid, or the file is not open for writing.
ENOSPC | No space is left on the device.
EOS2ERR | The call to the operating system was not successful.

Example
This example writes the contents of the character array buffer to the output file whose handle is fh.

    #include <io.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <string.h>

    #define FILENAME  "write.dat"

    int main(void)
    {
       int fh;
       char buffer[20];

       memset(buffer, 'a', 20);                 /* initialize storage */
       printf("\nCreating " FILENAME ".\n");
       system("echo Sample Program > " FILENAME);
       if (-1 == (fh = open(FILENAME, O_RDWR|O_APPEND))) {
          perror("Unable to open " FILENAME);
          return EXIT_FAILURE;
       }
       if (5 != _write(fh, buffer, 5)) {
          perror("Unable to write to " FILENAME);
          close(fh);
          return EXIT_FAILURE;
       }
       printf("Successfully appended 5 characters.\n");
       close(fh);
       return 0;

       /****************************************************************
          The program should create a file "write.dat" containing:

          Sample Program
          aaaaa

          The output should be:

          Creating write.dat.
          Successfully appended 5 characters.
       ****************************************************************/
    }

creat -- Create New File
fread -- Read Items
fwrite -- Write Items
lseek -- Move File Pointer
open -- Open File
read -- Read Into Buffer
_sopen -- Open Shared File
_tell -- Get Pointer Position
<io.h>