Posted on
2023-03-14 07:03乔55
阅读(118)
评论(0)
编辑收藏举报
read详解
int ret = ssize_t read(int fd, void *buf, size_t count);
// ret == -1 :出错,并设置errno// ret == -1 && errno == EAGAINA:// 不是read失败,而是以非阻塞方式读设备或网络文件,且文件无数据,直接返回// 0 :文件读取完毕// >0 :读取到的字节数
read() attempts to read up to count bytes from fd into the buffer starting at buf,On files that support seeking, the read operation commences at the current file offset, and the file offset is incremented by the number of bytes read. If the current file offset is at or past the end of file, no bytes are read, and read() returns zero.
// 尝试从fd读满count个字节到一个以buf开始的缓冲区// 对于支持寻址的文件,读操作从当前文件偏移量开始,且文件偏移量随读取的字节数增长// 如果当前文件偏移量刚好在文件末尾或超过末尾,将读取到0个字节,且返回0
If count is zero, read() may detect the errors described below. In the absence of any errors, or ifread() does not check for errors,a read() with a count of 0 returns zero and has no other effects.If count is greater than SSIZE_MAX, the result is unspecified.
// 如果count是0,read可能检测下面所描述的错误。如果没有错误或者没有检测错误,// count为0的read返回0,且没有其他影响。// 描述的错误:如果conut大于SSIZE_MAX,则结果是未确定的
On success, the number of bytes read is returned(zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now(maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal),or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position(if any) changes.
// 成功返回读取的字节数(返回0表示到了文件末尾),且文件指针位置向后移动number个位置。// 当返回值小于指定的字节数count时并不意味错误,这可能是因为文件当前可读取的字节数// 小于指定的count字节数:如已经接近文件末尾,或正从管道,终端读取数据或read被信号中断// 错误时返回-1,并设置合适的errno值。这种情况下无法得知文件偏移位置是否发生变化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)