The read() system call reads data from the open file referred to by the descriptor fd.
#include <unistd.h>
ssize_t read
(int fd, void *buffer, size_t count);
Returns number of bytes read, 0 on EOF, or -1 on error
The count argument specifies the maximum number of bytes to read. (The size_t data type is an unsigned integer type.) The buffer argument supplies the address of the memory buffer into which the input data is to be placed. This buffer must be at least count bytes long.
A successful call to read() returns the number of bytes actually read, or 0 if end-of-file is encountered. On error, the usual -1 is returned. The ssize_t data type is a signed integer type used to hold a byte count or a -1 error indication.
#define MAX_READ 20 char buffer[MAX_READ]; if (read(STDIN_FILENO, buffer, MAX_READ) == -1) errExit("read"); printf("The input data was: %s\n", buffer);
The output from this piece of code is likely to be strange, since it will probably include characters in addition to the string actually entered. This is because read() doesn’t place a terminating null byte at the end of the string that printf() is being asked to print. A moment’s reflection leads us to realize that this must be so, since read() can be used to read any sequence of bytes from a file. In some cases, this input might be text, but in other cases, the input might be binary integers or C structures in binary form. There is no way for read() to tell the difference, and so it can’t attend to the C convention of null terminating character strings. If a terminating null byte is required at the end of the input buffer, we must put it there explicitly:
char buffer[MAX_READ + 1]; ssize_t numRead; numRead = read(STDIN_FILENO, buffer, MAX_READ); if (numRead == -1) errExit("read"); buffer[numRead] = '\0'; printf("The input data was: %s\n", buffer);
Because the terminating null byte requires a byte of memory, the size of buffer must be at least one greater than the largest string we expect to read.