read (C System Call)

read is a system call used to read data into a buffer.

Required Include Files

#include <unistd.h>

Function Definition

ssize_t read(int fildes, void *buf, size_t nbytes);
Field Description
int fildes The file descriptor of where to read the input. You can either use a file descriptor obtained from the open system call, or you can use 0, 1, or 2, to refer to standard input, standard output, or standard error, respectively.
const void *buf A character array where the read content will be stored.
size_t nbytes The number of bytes to read before truncating the data. If the data to be read is smaller than nbytes, all data is saved in the buffer.
return value Returns the number of bytes that were read. If value is negative, then the system call returned an error.

Code Snippet

#include <unistd.h>
int main()
    char data[128];
    if(read(0, data, 128) < 0)
     write(2, "An error occurred in the read.\n", 31);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License