Rio packet Robust I/O function code _php Tutorial

Source: Internet
Author: User
[PHP]
Here is a

Here's about [PHP]
#include
#include
#include
#include
#include
#include

#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-&GT;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-&GT;RIO_FD, rp->rio_buf,sizeof (RP-&GT;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-&GT;RIO_BUFPTR,CNT);
Rp->rio_bufptr + = cnt;
RP-&GT;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: ' + ' */
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 (&AMP;RIO,FD);
while ((n = rio_readlineb (&rio,buf,maxline))! = 0) {
Rio_writen (1,buf,n);
}
Close (FD);
return 0;
}

#include
#include
#include
#include
#include
#include

#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-&GT;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-&GT;RIO_FD, rp->rio_buf,sizeof (RP-&GT;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-&GT;RIO_BUFPTR,CNT);
Rp->rio_bufptr + = cnt;
RP-&GT;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: ' + ' */
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;
}


The above function-type Rio package provides two different types of functions:

1. Input/output function without buffering

2. Input function with buffering

This is mainly for the file IO, because the standard IO itself is buffered, we in the read and write operation of the file is generally preferred to use the standard IO function, but in network programming, can only use the file IO function, here may be used in our above function writing, interested can learn more about the above mechanism. In the network communication, this kind of mechanism method application still is more!

http://www.bkjia.com/PHPjc/477281.html www.bkjia.com true http://www.bkjia.com/PHPjc/477281.html techarticle [PHP] Below is about [PHP] #include stdio.h #include string.h #include errno.h #include sys/types.h #include fcntl.h #incl Ude sys/stat.h #define MAXLINE/*unbu ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.