Linux c 源码(readn:从fd中读取n字节数据)

来源:互联网 发布:维普数据库论文检索 编辑:程序博客网 时间:2024/05/21 10:15
/* Read "n" bytes from a descriptor. */readn(int fd, void *vptr, size_t n){    size_t  nleft;    ssize_t nread;    char    *ptr;    ptr = vptr;    nleft = n;    while (nleft > 0) {        if ( (nread = read(fd, ptr, nleft)) < 0) {            if (errno == EINTR)                nread = 0;      /* and call read() again */            else                return(-1);        } else if (nread == 0)            break;              /* EOF */        nleft -= nread;        ptr   += nread;    }    return(n - nleft);      /* return >= 0 */}/* end readn */ssize_tReadn(int fd, void *ptr, size_t nbytes){    ssize_t     n;    if ( (n = readn(fd, ptr, nbytes)) < 0)        err_sys("readn error");    return(n);}

使用的时候使用函数Readn,Readn是个包裹函数,编程的时候就不需要对函数进行错误处理了。

1 0
原创粉丝点击