Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>write() write data to a file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
write()                  Write Data to a File

 #include   <io.h>                       Required for declarations only

 int            write(handle, buffer, count);
 int           handle;                   Handle referring to open file
 char          *buffer;                  Data storage location
 unsigned int  count;                    Number of bytes

    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. After the write
    operation, the file pointer is increased by the number of bytes
    actually written.

    Returns:    The number of bytes actually written, or -1 if an error
                occurs. On error, 'errno' (defined in <stdlib.h>) is set
                to one of the following:

                    EACCES:      File is read only or locked against writing
                    EBADF:       Invalid file handle
                    ENOSPC       No space left on disk

      Notes:    The return value may be less than 'count', but still
                positive. For example, disk space may run out before
                'count' bytes are written.

                If writing more than 32k bytes, the return value should
                be of type unsigned int. The maximum number of bytes that
                can be written to a file is 65534, since 65535 is
                indistinguishable from -1.

                The CR-LF translations of files open in text mode does
                not affect the return value.

  -------------------------------- Example ---------------------------------

    This example opens a file and writes to it.

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

          char buffr[10000];

          main()
          {
                int fhndl;
                int count, totlbytes;

                 count = 1000;
                 if ((fhndl = open("inv.dat",O_APPEND)) == -1) {
                    perror("can't open output file");
                    exit(1);
                 }
                 totlbytes = write(fhndl,buffr,count);
          }

See Also: fwrite() open() read()

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