RIO包 健壮的I/O函数代码
下面是关于
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h> #define MAXLINE 1024 /*unbuffer input/output function*/ ssize_t rio_readn(int fd,void *usrbuf,size_t n)
{
size_t nleft = n;
ssize_t nread;
char *bufp = usrbuf; while(nleft > 0){
if((nread = read(fd,bufp,nleft)) < 0){
if(errno == EINTR){/*interrupted by sig handler return*/
nread = 0;
}else{
return -1;/*error*/
}
}else if(nread == 0){
break; /*EOF*/
}else{/*read content*/
nleft -= nread;
bufp += nread;
}
}
return (n - nleft);
} ssize_t rio_writen(int fd,void *usrbuf,size_t n)
{
size_t nleft = n;
ssize_t nwritten;
char *bufp = usrbuf; while(nwritten = write(fd,bufp,nleft) <= 0){
if(errno == EINTR){
nwritten = 0;
}else{
return -1;
}
nleft -= nwritten;
bufp += nwritten;
}
return n;
}
/******************************************************************************/
#define RIO_BUFSIZE 8192
typedef struct{
int rio_fd; /*To operate the file descriptor*/
int rio_cnt;/*unread bytes in internal buf*/
char *rio_bufptr;/*next unread byte int internal buf*/
char rio_buf[RIO_BUFSIZE];/*internal buf*/
}rio_t;
void rio_readinitb(rio_t *rp,int fd)
{
rp->rio_fd = fd;
rp->rio_cnt = 0;
rp->rio_bufptr = rp->rio_buf;
}
static ssize_t rio_read(rio_t *rp,char *usrbuf,size_t n)
{
int cnt;
while(rp->rio_cnt <= 0){/*Read the file content if buf is empty*/
rp->rio_cnt = read(rp->rio_fd, rp->rio_buf,sizeof(rp->rio_buf));
if(rp->rio_cnt < 0){
if(errno != EINTR){
return -1;
}
}else if(rp->rio_cnt == 0){/*EOF*/
return 0;
}else {/*reset buf ptr*/
rp->rio_bufptr = rp->rio_buf;
}
}
/*when n < rp->rio_cnt, need copy some times */
cnt = n;
if(rp->rio_cnt < n){/*one time copy end*/
cnt = rp->rio_cnt;
}
memcpy(usrbuf,rp->rio_bufptr,cnt);
rp->rio_bufptr += cnt;
rp->rio_cnt -= cnt;
return cnt;
}
ssize_t rio_readlineb(rio_t *rp, void *usrbuf,size_t maxlen)
{
int n,rc;
char c,*bufp = usrbuf;
for(n = 1; n < maxlen; n++){
if (( rc = rio_read(rp,&c,1)) == 1){
*bufp++ = c;
if(c == '\n'){
break;
}
}else if (rc == 0){
if(n == 1){/*EOF no data read*/
return 0;
}else{/*EOF some data read*/
break;
}
}else{/*ERROR*/
return -1;
}
}
*bufp = 0;/*string end sign :'\0'*/
return n;
} ssize_t rio_readnb(rio_t *rp,void *usrbuf,size_t n)
{
size_t nleft = n;
ssize_t nread;
char *bufp = usrbuf; while(nleft > 0){
if((nread = rio_read(rp,bufp, nleft)) < 0){
if(errno == EINTR){/*interrupted by sig handler return*/
nread =0;
}else{/*errno set by read() */
return -1;
}
}else if(nread == 0){/*EOF*/
break;
}
nleft -= nread;
bufp += nread;
}
return (n-nleft);/*return >=0*/
} int main()
{
int n;
rio_t rio;
char buf[MAXLINE]; int fd = open("1.txt",O_RDONLY,755);
if(fd <=0){
printf("error\n");
}
rio_readinitb(&rio,fd);
while ((n = rio_readlineb(&rio,buf,MAXLINE)) != 0){
rio_writen(1,buf,n);
}
close(fd);
return 0;
}
上面的函数式RIO包提供的两类不同的函数:
1、不带缓冲的输入输出函数
2、带缓冲的输入函数
这里主要针对文件Io,因为标准Io本身就是带缓冲的,我们在对文件进行读写操作时,一般会优先使用标准的Io函数,但是在网络编程中,只能使用文件Io的函数,这里可能就用到我们上面的函数编写,有兴趣的可以多了解下上面的机制。在网络通信中,这种机制方法运用还是比较多的!
RIO包 健壮的I/O函数代码的更多相关文章
- 健壮的网络编程IO函数-RIO包
RIO包 简介 Rio包即为Robust io函数包.包中函数是对Linux基本I/O函数的封装,使其更加健壮.高效,更适用于网络编程. 分析 Rio包由rio_t结构体和系列函数组成. 首先是两个不 ...
- Linux IO操作——RIO包
1.linux基本I/O接口介绍 ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, void *buf, siz ...
- Maxim实时时钟芯片设计指南5791-关于编写健壮的实时时钟控制代码的提示
用DS12C887设计一个万年历,虽然反复查看说明书,还是出各种的错误. 因此,从美信官网查询资料,翻译的不太通,凑合着对照看. 原文链接 Tips for Writing Bulletproof R ...
- ThinkPHP3.2 G函数代码及 使用方法
ThinkPHP3.2 G函数代码及 使用方法 代码: // 内存是否可调用 define('MEMORY_LIMIT_ON',function_exists('memory_get_usage')) ...
- php 安全过滤函数代码
php 安全过滤函数代码,防止用户恶意输入内容. //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_r ...
- 使用正则表达式匹配JS函数代码
使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...
- C语言实现md5函数代码
网上找到的实现md5函数代码,包括一个头文件md5.h和一个源文件md5.c,用下面的测试代码test.c测试通过,各文件依次如下: 头文件md5.h #ifndef MD5_H #define MD ...
- 前端JS面试题汇总 Part 2 (null与undefined/闭包/foreach与map/匿名函数/代码组织)
原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questio ...
- (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析
转自:http://nkwavelet.blog.163.com/blog/static/2277560382013103010312144/ x264版本: x264-snapshot-2014 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0806平均分
题目
- hdu 3917 (最大权闭合图)
题意:政府有一些路,m个公司来修,每个公司修路要交税给政府,修路政府要付给公司费用,求政府能获得的最大利润,如果选择一个公司负责一个项目,那么该公司负责的其它项目也必须由他负责,并且与其有相连关系的公 ...
- Oracle cloud control 12c 怎样改动sysmanpassword
前阵子在虚拟机部署了Oracle Cloud Control 12c.事别几日,居然忘记了登录password. 主要是由于如今的Oracle有关的Software比之前提供更强的安全机制.什 ...
- anroid里面的post请求
一.需要用到的场景 在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了. 二.需要用到的主要类 在a ...
- 配置SAP 采购合同审批
需求: 采购合同类型是MK,采购组织是POSC,采购组PGC,标识:估计价格是空,总价有值0.00 - 9999999999.00 RMB 满足以上条件的时候需要审批该合同. 配置: spro-> ...
- Eclipse验证码
package MyEclipse; import java.io.*; public class MyEclipseGen { private static final String ...
- 查询mysql哪些表正在被锁状态
1.查进程,主要是查找被锁表的那个进程的ID SHOW PROCESSLIST; 2.kill掉锁表的进程ID KILL 10866;//后面的数字即时进程的ID
- 九度OnlineJudge之1017:还是畅通工程
题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...
- ioctl、文件操作接口函数以及nand的升级模式的操作过程详解
概述 内核中驱动文件的操作通常是通过write和read函数进行的,但是很多时候再用户空间进行的操作或许不是内核中公共代码部分提供的功能,此时就需要使用一种个性化的方法进行操作--ioctl系统调用. ...
- 基于visual Studio2013解决C语言竞赛题之1088模拟计算器
题目 解决代码及点评 /************************************************************************/ /* ...