_read, _dos_read
Reads from file.
Defined in header <io.h>
Syntax
#include <io.h>
int _read(int handle, void *buf,unsigned len);
#include <dos.h>
unsigned _dos_read(int handle, void far *huf,unsigned *nread);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ |
Remarks
_read attempts to read /e« bytes from the file associated with handle into the buffer pointed to by huf.
When a file is opened in text mode, _read does not remove carriage returns.
_dos_read uses DOS function 0x3F to read len bytes from the file associated with handle into the buffer pointed to by the far pointer huf. The actual number of bytes read is stored at the location pointed to by mead; when an error occurs, or the end-of-file is encountered, this number may be less than len.
_dos_read does not remove carriage returns because all its files are binary files.
handle is a file handle obtained from a _dos_creat, _dos_creatnew, or _dos_open call.
For _read, handle is a file handle obtained from a creat, open, dup, or dup2 call.
On disk files, _dos_read and _read begin reading at the current file pointer. When the reading is complete, they increment the file pointer by the number of bytes read. On devices, the bytes are read directly from the device.
| The maximum | number of bytes that _dos_read or _read can read is |
65,534, because 65,535 (0xFFFF) is the same as -1, the error return indicator.
Return value
On successful completion, _dos_read returns 0. Otherwise, the function returns the DOS error code and sets the global variable errno.
On successful completion, _read returns a positive integer indicating the number of bytes placed in the buffer. On end-of-file, _read returns zero. On error, it returns -1, and the global variable errno.
. The global variable errno is set to one of the following:
| EACCES | Permission denied |
| EBADF | Bad file number |
See also
Example
#include <stdio.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
int main(void) /* Example for _read. */
{
void *buf;
int handle, bytes;
buf = malice (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.$$$ *l
if ((handle = open( "TEST.$$$", 0_RDONLY )) == -1) {
printf ("Error Opening File\n");
exit(l);
}
if ((bytes = _read(handle, buf, 10)) == -1) {
printf ("Read Failed.\n");
exit(l);
}
else printf ("Read: %d bytes read.\n", bytes);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <dos.h->
int main(void) /* Example for _dos_read. */
{
int handle;
unsigned bytes;
char buf[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.$$$ V
if .(_dos_open("TEST.$$$", 0_RDONLY, &handle) != 0) {
perror("Unable to open TEST. $$$");
return 1;
}
if (_dos_read (handle, buf, 10, &bytes) != 0) {
perror("Unable to read from TEST.$$$");
return 1;
}
else printf ("_dos_read: %d bytes read.\n", bytes);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/io.h/_read/
· short form /3.1/_read/