如果我没有流但有文件描述符,我该如何使用 fgets 或 getline?
How do I use fgets or getline if I don't have a stream but a file descriptor?
如果我没有流但有文件描述符,如何使用 fgets 或 getline?
根据 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.
您可以使用 read() 函数从文件描述符中读取,这非常清楚,来自手册页:
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.
如果我没有流但有文件描述符,如何使用 fgets 或 getline?
根据 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.
您可以使用 read() 函数从文件描述符中读取,这非常清楚,来自手册页:
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.