Reads bytes from a file and places them in a buffer.
#include <unistd.h>
ssize_t read (int file_desc, void *buffer, size_t
nbytes); (ISO
POSIX-1)
int read (int file_desc, void *buffer,
int nbytes); (Compatability)
| n | The number of bytes read. |
| -1 | Indicates a read error, including physical input errors, illegal buffer addresses, protection violations, undefined file descriptors, and so forth. |
#include <file.h>
#include <unistd.h>
main()
{
int fd,i;
char buf[10];
if ( (fd=open("test.txt",O_RDWR,0,"shr=upd")) <= 0 )
{
perror("open");
exit();
}
/* Read 2 characters into buf. */
if ( (i=read(fd, buf, 2)) < 0)
{
perror("read");
exit();
}
/* Print out what was read. */
if( i > 0)
printf("buf='%c%c'\n",buf[0],buf[1]);
close(fd);
}