RAD Studio |  |

_rtl_write

Header File

io.h

Category

Input/output Routines

Prototype

int _rtl_write(int handle, void *buf, unsigned len);

Description

Writes to a file.

Note: This function replaces _write which is obsolete.
_rtl_write attempts to write len bytes from the buffer pointed to by buf to the file associated with handle.

The maximum number of bytes that _rtl_write can write is UINT_MAX -1 (because UINT_MAX is the same as -1), which is the error return indicator for _rtl_write. UINT_MAX is defined in limits.h. _rtl_write does not translate a linefeed character (LF) to a CR/LF pair because all its files are binary files.

If the number of bytes actually written is less than that requested the condition should be considered an error and probably indicates a full disk.

For disk files, writing always proceeds from the current file pointer. On devices, bytes are directly sent to the device.

For files opened with the O_APPEND option, the file pointer is not positioned to EOF before writing the data.

Return Value

On success, _rtl_write returns number of bytes written.

On error, it returns -1 and sets the global variable errno to one of the following values:

EACCES | Permission denied
EBADF | Bad file number

Example

Copy Code

    #include <stdio.h>
    #include <io.h>
    #include <alloc.h>
    #include <fcntl.h>
    #include <process.h>
    #include <sys\stat.h>
    int main(void)
    {
       void *buf;
       int handle, bytes;
       buf = malloc(200);
    /*
    Create a file name TEST.$$$ in the current directory and writes 200 bytes to it. If TEST.$$$ already exists, it's overwritten.
     */
       if ((handle = open("TEST.$$$", O_CREAT | O_WRONLY | O_BINARY,
                           S_IWRITE | S_IREAD)) == -1)
       {
          printf("Error Opening File\n");
          exit(1);
       }
       if ((bytes = _rtl_write(handle, buf, 200)) == -1) {
          printf("Write Failed.\n");
          exit(1);
       }
       printf("_rtl_write: %d bytes written.\n",bytes);
       return 0;
    }

Portability

POSIX | Win32 | ANSI C | ANSI C++
| + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!