Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ v10.0 : C library - <b>synopsis:</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <io.h>
    int write( int handle, void *buffer, unsigned len );

Description:
    The write function writes data at the operating system level.  The
    number of bytes transmitted is given by len and the data to be
    transmitted is located at the address specified by buffer.

    The handle value is returned by the  open function.  The access mode
    must have included either O_WRONLY or O_RDWR when the  open function was
    invoked.

    The data is written to the file at the end when the file was opened with
    O_APPEND included as part of the access mode; otherwise, it is written
    at the current file position for the file in question.  This file
    position can be determined with the  tell function and can be set with
    the  lseek function.

    When O_BINARY is included in the access mode, the data is transmitted
    unchanged.  When O_TEXT is included in the access mode, the data is
    transmitted with extra carriage return characters inserted before each
    linefeed character encountered in the original data.

    A file can be truncated under DOS and OS/2 2.0 by specifying 0 as the
    len argument.  Note, however, that this doesn't work under OS/2 2.1,
    Windows NT, and other operating systems.  To truncate a file in a
    portable manner, use the  chsize function.

Returns:
    The write function returns the number of bytes (does not include any
    extra carriage-return characters transmitted) of data transmitted to the
    file.  When there is no error, this is the number given by the len
    argument.  In the case of an error, such as there being no space
    available to contain the file data, the return value will be less than
    the number of bytes transmitted.  A value of -1 may be returned in the
    case of some output errors.  When an error has occurred,  errno contains
    a value indicating the type of error that has been detected.

See Also:
    chsize, close, creat, dup, dup2, eof, exec Functions Functions, filelength,
    fileno, fstat, isatty, lseek, open, read, setmode, sopen, stat, tell,
    umask

Example:
    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>

    char buffer[]
            = { "A text record to be written" };

    void main()
      {
        int handle;
        int size_written;

        /* open a file for output             */
        /* replace existing file if it exists */
        handle = open( "file",
                    O_WRONLY | O_CREAT | O_TRUNC | O_TEXT,
                    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
        if( handle != -1 ) {

          /* write the text                     */
          size_written = write( handle, buffer,
                                sizeof( buffer ) );

          /* test for error                     */
          if( size_written != sizeof( buffer ) ) {
              printf( "Error writing file\n" );
          }

          /* close the file                     */
          close( handle );
        }
      }

Classification:
    POSIX 1003.1

Systems:
    All

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson