加入收藏 | 设为首页 | 会员中心 | 我要投稿 佛山站长网 (https://www.0757zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux编程:readn, writen和readline函数的调用

发布时间:2016-11-11 09:37:46 所属栏目:Linux 来源:网络整理
导读:#include unistd.h#include errno.h ssize_t readn(int fd, void *buf, size_t count){ char *strtmp; ssize_t reval, realcount=0; strtmp = (char *)buf; whi
#include <unistd.h>
#include <errno.h>
    
ssize_t readn(int fd, void *buf, size_t count)
{
  char *strtmp;
  ssize_t reval, realcount=0;
  strtmp = (char *)buf;
      
  while (count>0)
  {
    reval = read(fd, strtmp, count);
    if (reval<0)
      if (errno == EINTR)
        continue;
      else return -1;
    else if (reval>0)
    {
      count -= reval;
      strtmp += reval;
      realcount += reval;
      continue;
    }
    else break;
  }
      
  return realcount;
}
    
ssize_t writen(int fd, const void *buf, size_t count)
{
  char *strtmp;
  ssize_t reval, realcount=count;
  strtmp = (char *)buf;
      
  while(count>0)
  {
    reval = write(fd, strtmp, count);
    if (reval < 0)
      if (errno == EINTR)
        continue;
      else return -1;
          
    count -= reval;
    strtmp += reval;
  }
      
  return realcount;
}
ssize_t readline(int fd, void *buf, int size)
{
  char *strtmp;
  ssize_t reval, realcount=0;
  strtmp = (char *)buf;
     
  while(size>1)
  {
    reval = read(fd, strtmp, 1);
    if (reval<0)
      if (errno == EINTR)
        continue;
      else return -1;
    else if (reval == 0)
      break;
    else
    {
      realcount++;
      size--;
      if (*strtmp++ =='n')
        break;
    }
  }
  *strtmp='';
  return realcount;
}

(编辑:佛山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读