DosWrite

From EDM2
Revision as of 19:29, 21 June 2016 by Martini (Talk | contribs)

Jump to: navigation, search

Description

Writes a specified number of bytes from a buffer to the specified file.

Syntax

#define INCL_DOSFILEMGR
#include <os2.h>

HFILE     hFile;      /*  File handle from DosOpen. */
PVOID     pBuffer;    /*  Address of the buffer that contains the data to write. */
ULONG     cbWrite;    /*  Number of bytes to write. */
PULONG    pcbActual;  /*  Address of the variable to receive the number of bytes actually written. */
APIRET    ulrc;       /*  Return Code. */

ulrc = DosWrite(hFile, pBuffer, cbWrite, pcbActual);

Parameters

hFile (HFILE) - input 
File handle from DosOpen.
pBuffer (PVOID) - input 
Address of the buffer that contains the data to write.
cbWrite (ULONG) - input 
Number of bytes to write.
pcbActual (PULONG) - output 
Address of the variable to receive the number of bytes actually written.

Return Code

ulrc (APIRET) - returns


DosWrite returns one of the following values:

  • 0 NO_ERROR
  • 5 ERROR_ACCESS_DENIED
  • 6 ERROR_INVALID_HANDLE
  • 19 ERROR_WRITE_PROTECT
  • 26 ERROR_NOT_DOS_DISK
  • 29 ERROR_WRITE_FAULT
  • 33 ERROR_LOCK_VIOLATION
  • 109 ERROR_BROKEN_PIPE
  • 112 ERROR_DISK_FULL
  • 157 ERROR_DISCARDED

Remarks

Note: When writing message pipes the application is limited to 64K messages. As well, these messages cannot span 64k boundaries due to the current design of the thunk layer in read or write routines. If the message is not written in an aligned manner, the subsequent read will not be able to handle the messages properly. If a 64k or less message is written to a pipe from an aligned buffer, the read will handle this properly.

DosWrite begins to write at the current file-pointer position. The file pointer is automatically moved by read and write operations. It can be moved to a desired position by issuing DosSetFilePtr.

If the specified file has been opened using the write-through flag, DosWrite writes the data to the disk before returning. Upon return to the caller, pcbActual contains the number of bytes actually written.

If there is not enough space on the disk or diskette to write all of the bytes specified by cbWrite then DosWrite does not write any bytes. Upon return to the caller, pcbActual contains zero.

A value of zero for cbWrite is not considered an error. No data transfer occurs, and there is no effect on the file or the file pointer.

If the file is read-only, the write operation to the file is not performed.

If you issue DosOpen with the Direct Open flag set to 1 in the fsOpenMode parameter, you have direct access to an entire disk or diskette volume, independent of the file system. You must lock the logical volume before accessing it, and you must unlock the logical volume when you are finished accessing it. Issue DosDevIOCtl for Category 8, DSK_LOCKDRIVE to lock the logical volume, and for Category 8, DSK_UNLOCKDRIVE to unlock the logical volume. While the logical volume is locked, no other process can access it.

Named-Pipe Considerations

DosWrite also is used to write bytes or messages to a named pipe.

Each write operation to a message pipe writes a message whose size is the length of the write operation. DosWrite automatically encodes message lengths in the pipe, so applications need not encode this information in the buffer being written.

Write operations in blocking mode always write all requested bytes before returning.

In nonblocking mode, DosWrite returns either with all bytes written or none written. DosWrite returns with no bytes written when it would have to divide the message into blocks in order to complete the request. This can occur when there is not enough space left in the pipe, or when the pipe is currently being written to by another client. If this occurs, DosWrite returns immediately with a value of zero for pcbActual, indicating that no bytes were written.

For a byte pipe, if the number of bytes to be written exceeds the space available in the pipe, DosWrite writes as many bytes as it can, and returns with the number of bytes actually written in pcbActual.

An attempt to write to a pipe whose other end has been closed returns ERROR_BROKEN_PIPE or, if the other end was closed without reading all pending data, ERROR_DISCARDED.

Clients of named pipes created with the NP_ACCESS_OUTBOUND access mode cannot use the DosWrite function. If the named pipe's client uses the DosWrite function, the function returns error code ERROR_ACCESS_DENIED.

Example Code

This example writes a string to a file named "DOSTEST", positions the file pointer back to the beginning of the file, and read the string written. It also opens and closes the file.

}
#define INCL_DOSFILEMGR          /* File Manager values */
#define INCL_DOSERRORS           /* DOS Error values    */
#include <os2.h>
#include <stdio.h>
#include <string.h>

int main(void) {
   HFILE  hfFileHandle   = 0L;     /* Handle for file being manipulated */
   ULONG  ulAction       = 0;      /* Action taken by DosOpen */
   ULONG  ulBytesRead    = 0;      /* Number of bytes read by DosRead */
   ULONG  ulWrote        = 0;      /* Number of bytes written by DosWrite */
   ULONG  ulLocal        = 0;      /* File pointer position after DosSetFilePtr */
   UCHAR  uchFileName[20]  = "dostest.dat",     /* Name of file */
          uchFileData[100] = " ";               /* Data to write to file */
   APIRET rc             = NO_ERROR;            /* Return code */

   /* Open the file test.dat.  Use an existing file or create a new */
   /* one if it doesn't exist.                                      */
   rc = DosOpen(uchFileName,                    /* File path name */
                &hfFileHandle,                  /* File handle */
                &ulAction,                      /* Action taken */
                100L,                           /* File primary allocation */
                FILE_ARCHIVED | FILE_NORMAL,    /* File attribute */
                OPEN_ACTION_CREATE_IF_NEW |
                OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type */
                OPEN_FLAGS_NOINHERIT |
                OPEN_SHARE_DENYNONE  |
                OPEN_ACCESS_READWRITE,          /* Open mode of the file */
                0L);                            /* No extended attribute */

   if (rc != NO_ERROR) {
      printf("DosOpen error: return code = %u\n", rc);
      return 1;
   } else {
     printf ("DosOpen: Action taken = %ld\n", ulAction);
   } /* endif */
   /* Write a string to the file */
   strcpy (uchFileData, "testing...\n1...\n2...\n3\n");

   rc = DosWrite (hfFileHandle,                /* File handle */
                  (PVOID) uchFileData,         /* String to be written */
                  sizeof (uchFileData),        /* Size of string to be written */
                  &ulWrote);                   /* Bytes actually written */

   if (rc != NO_ERROR) {
      printf("DosWrite error: return code = %u\n", rc);
      return 1;
   } else {
      printf ("DosWrite: Bytes written = %u\n", ulWrote);
   } /* endif */

   /* Move the file pointer back to the beginning of the file */
   rc = DosSetFilePtr (hfFileHandle,           /* File Handle */
                       0L,                     /* Offset */
                       FILE_BEGIN,             /* Move from BOF */
                       &ulLocal);              /* New location address */
   if (rc != NO_ERROR) {
      printf("DosSetFilePtr error: return code = %u\n", rc);
      return 1;
   }

   /* Read the first 100 bytes of the file */
   rc = DosRead (hfFileHandle,                /* File Handle */
                 uchFileData,                 /* String to be read */
                 100L,                        /* Length of string to be read */
                 &ulBytesRead);               /* Bytes actually read */

   if (rc != NO_ERROR) {
      printf("DosRead error: return code = %u\n", rc);
      return 1;
   } else {
      printf ("DosRead: Bytes read = %u\n%s\n", ulBytesRead, uchFileData);
   } /* endif */

   rc = DosClose(hfFileHandle);                /* Close the file */

   if (rc != NO_ERROR) {
      printf("DosClose error: return code = %u\n", rc);
      return 1;
   }
   return NO_ERROR;
}

Related Functions