Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ write(DOS) — OpenDesktop Software Development System 1.0.0d

Media Vault

Software Library

Restoration Projects

Artifacts Sought

     Name
          write - Writes to a file.

     Syntax
          #include <io.h>

          int write(handle, buffer, count)
          int handle;
          char *buffer;
          unsigned int count;

     Description
          The write function writes count bytes from buffer into the
          file associated with handle. The write operation begins at
          the current position of the file pointer (if any) associated
          with the given file. If the file is open for appending, the
          operation begins at the current end of the file. After the
          write operation, the file pointer is increased by the number
          of bytes actually written.

     Return Value
          The write function returns the number of bytes actually
          written. The return value may be positive but less than
          count (for example, when write runs out of disk space before
          count bytes are written).

          A return value of -1 indicates an error. In this case, errno
          is set to one of the following values:

            Value     Meaning

            EBADF     Invalid file handle or file not opened for writing

            ENOSPC    No space left on device

          If you are writing more than 32K (the maximum size for type
          int) to a file, the return value should be of type unsigned
          int. (See the example that follows.) However, the maximum
          number of bytes that can be written to a file at one time is
          65,534, since 65,535 (or 0xFFFF) is indistinguishable from
          -1, and so would return an error.

          If the file is 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.

     See Also
          fwrite(DOS), open(DOS), read(DOS)

     Notes
          When writing to files opened in text mode, the write
          function treats a CTRL+Z character as the logical end-of-
          file. When writing to a device, write treats a CTRL+Z
          character in the buffer as an output terminator.

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

          char buffer[6000] = "This is a test of 'write' function";

          main()
           {
                  int fh;
                  unsigned int nbytes = 60000, byteswritten;

                  if ((fh = open("c:/data/conf.dat",O_WRONLY)) == -1)
                  {
                          perror("Open failed on output file");
                          exit(1);
                  }

                  if ((byteswritten = write(fh, buffer, nbytes)) ==
          -1)
                          perror("");
                  else
                          printf("Wrote %u bytes to file\n",
          byteswritten);
           }

          This program opens a file for output and uses write to write
          60,000 bytes to the file.

                                                (printed 6/18/89)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026