Search code examples
clinuxubuntuposixgnu

How do I use fgets or getline if I don't have a stream but a file descriptor?


How do I use fgets or getline if I don't have a stream but a file descriptor ?

According to manpage of fgets

It is not advisable to mix calls to input functions from the stdio library with low-level calls to read(2) for the file descriptor associated with the input stream; the results will be undefined and very probably not what you want.


Solution

  • you can use read() function to read from file descriptor and it's very clear, from man page:

    read(int fd, void *buf, size_t count); read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.