Visual Studio 6.0

Reads data from a file.

int _read( int handle, void *buffer, unsigned int count );

Routine Required Header Compatibility
_read <io.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_read returns the number of bytes read, which may be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–linefeed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.

If the function tries to read at end of file, it returns 0. If the handle is invalid, or the file is not open for reading, or the file is locked, the function returns –1 and sets errno to EBADF.



Handle referring to open file


Storage location for data


Maximum number of bytes


The _read function reads a maximum of count bytes into buffer from the file associated with handle. The read operation begins at the current position of the file pointer associated with the given file. After the read operation, the file pointer points to the next unread character.

If the file was opened in text mode, the read terminates when _read encounters a CTRL+Z character, which is treated as an end-of-file indicator. Use _lseek to clear the end-of-file indicator.


/* READ.C: This program opens a file named
 * READ.C and tries to read 60,000 bytes from
 * that file using _read. It then displays the
 * actual number of bytes read from READ.C.

#include <fcntl.h>      /* Needed only for _O_RDWR definition */
#include <io.h>
#include <stdlib.h>
#include <stdio.h>

char buffer[60000];

void main( void )
   int fh;
   unsigned int nbytes = 60000, bytesread;

   /* Open file for input: */
   if( (fh = _open( "read.c", _O_RDONLY )) == -1 )
      perror( "open failed on input file" );
      exit( 1 );

   /* Read in input: */
   if( ( bytesread = _read( fh, buffer, nbytes ) ) <= 0 )
      perror( "Problem reading file" );
      printf( "Read %u bytes from file\n", bytesread );

   _close( fh );


Read 775 bytes from file

Low-Level I/O Routines

See Also   _creat, fread, _open, _write

© 2015 Microsoft