Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo 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
 void          *buffer;                  Data storage location
 unsigned int  count;                    Number of bytes

    write() writes 'count' bytes from 'buffer' into the file associated
    with 'handle', beginning at the current position of the file pointer.
    After the write operation, the file pointer is increased by the
    number of bytes actually written.  If the handle is associated with a
    device, rather than a disk file, the bytes are written to the device.

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

                    EACCES:      File is read only or locked against writing
                    EBADF:       Invalid file handle

         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 to be written, the return
                    value should be of type unsigned int. The maximum
                    number of bytes that can be written to a file is
                    65534, because 65535 (0xFFFF) is indistinguishable
                    from -1.

                    CR-LF translations of files open in text mode do 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[20] = "abcdefghijklmnop";

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

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


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

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