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 6.0 - <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() takes 'count' bytes from 'buffer', writes them to the file
    associated with 'handle', and increases the file pointer by the
    number of bytes actually written.  Writing begins at the current
    position of the file pointer associated with the file.

    Returns:    The number of bytes actually written.  On error, -1 is
                returned and '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 more than 32k bytes are written, the return value
                should be of type unsigned int. 65535 is
                indistinguishable from -1, so the maximum number of bytes
                that can be written to a file is 65534.

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

 Portability:   Does not support ANSI standard.

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

    This example opens a file and writes to the end of it.

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

           char buffr[40] = "abcdefghijklmnopqrstuvwxyz";

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

                 count = 20;
                 if ((fhndl = open("inv.dat",O_RDWR)) == -1) {
                     perror("can't open output file");
                     exit(1);
                 }
                 lseek(fhndl,0L,SEEK_END);
                 if ((totlbytes = write(fhndl,buffr,count)) == -1)
                    perror("");
                 else
                    printf("total bytes written: %d\n",totlbytes);
           }


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

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