_rtl_read

De RAD Studio
Aller à : navigation, rechercher

Remonter à Io.h - Index


Header File

io.h

Category

Input/output Routines

Prototype

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

Description

Reads from file.

Remarque :  This function replaces _read which is obsolete.

This function reads len bytes from the file associated with handle into the buffer pointed to by buf. When a file is opened in text mode, _rtl_read does not remove carriage returns.

The argument handle is a file handle obtained from a creat, open, dup, or dup2 call.

On disk files, _rtl_read begins reading at the current file pointer. When the reading is complete, it increments the file pointer by the number of bytes read. On devices, the bytes are read directly from the device.

The maximum number of bytes it can read is UINT_MAX -1 (because UINT_MAX is the same as -1, the error return indicator). UINT_MAX is defined in limits.h.

Return Value

On success, _rtl_read returns either

  • a positive integer, indicating the number of bytes placed in the buffer
  • zero, indicating end-of-file

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



 #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(10);
 /*
 Looks for a file in the current directory named TEST.$$$ and attempts to read 10 bytes from it. To use this example you should create the file TEST.$$$
  */
    if ((handle =
        open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
    {
       printf("Error Opening File\n");
       exit(1);
    }
    if ((bytes = _rtl_read(handle, buf, 10)) == -1) {
       printf("Read Failed.\n");
       exit(1);
    }
    else {
       printf("_rtl_read: %d bytes read.\n", bytes);
    }
    return 0;
 }



Portability



POSIX Win32 ANSI C ANSI C++

+