当前位置: 技术问答>linux和unix
read len个字节
来源: 互联网 发布时间:2016-11-10
本文导语: while(1) { ret = read(fd, buf, len); if(ret == -1) { if(errno == EINTR) continue; perror("read"); break; } len -= ret; buf += ret; if(len == 0 || ret == 0) { break; ...
while(1)
{
ret = read(fd, buf, len);
if(ret == -1)
{
if(errno == EINTR)
continue;
perror("read");
break;
}
len -= ret;
buf += ret;
if(len == 0 || ret == 0)
{
break;
}
}
大家知道系统调用read存在太多情况了,一次调用可能被中断,可能遇到文件结束,可能阻塞等等,自己写了这段代码,企图读取len个字节到buf中,大家看看斧正一下,看有漏洞没?
|
apue上就有readn writen的实现示例
ssize_t /* Read "n" bytes from a descriptor */
readn(int fd, void *ptr, size_t n)
{
size_t nleft;
ssize_t nread;
nleft = n;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) = 0 */
}