write and read
2013-05-31 11:51
375 查看
int my_write(int fd,void *buffer,int length)
{
int bytes_left;
int written_bytes;
char *ptr;
ptr=buffer;
bytes_left=length;
while(bytes_left>0)
{
/* 开始写*/
written_bytes=write(fd,ptr,bytes_left);
if(written_bytes<=0) /* 出错了*/
{
if(errno==EINTR) /* 中断错误 我们继续写*/
written_bytes=0;
else /* 其他错误 没有办法,只好撤退了*/
return(-1);
}
bytes_left-=written_bytes;
ptr+=written_bytes; /* 从剩下的地方继续写 */
}
return(0);
}
int my_read(int fd,void *buffer,int length)
{
int bytes_left;
int bytes_read;
char *ptr;
bytes_left=length;
while(bytes_left>0)
{
bytes_read=read(fd,ptr,bytes_read);
if(bytes_read<0)
{
if(errno==EINTR)
bytes_read=0;
else
return(-1);
}
else if(bytes_read==0)
break;
bytes_left-=bytes_read;
ptr+=bytes_read;
}
return(length-bytes_left);
}
{
int bytes_left;
int written_bytes;
char *ptr;
ptr=buffer;
bytes_left=length;
while(bytes_left>0)
{
/* 开始写*/
written_bytes=write(fd,ptr,bytes_left);
if(written_bytes<=0) /* 出错了*/
{
if(errno==EINTR) /* 中断错误 我们继续写*/
written_bytes=0;
else /* 其他错误 没有办法,只好撤退了*/
return(-1);
}
bytes_left-=written_bytes;
ptr+=written_bytes; /* 从剩下的地方继续写 */
}
return(0);
}
int my_read(int fd,void *buffer,int length)
{
int bytes_left;
int bytes_read;
char *ptr;
bytes_left=length;
while(bytes_left>0)
{
bytes_read=read(fd,ptr,bytes_read);
if(bytes_read<0)
{
if(errno==EINTR)
bytes_read=0;
else
return(-1);
}
else if(bytes_read==0)
break;
bytes_left-=bytes_read;
ptr+=bytes_read;
}
return(length-bytes_left);
}
相关文章推荐
- Mysql read lock and write lock
- golang map引发的 【fatal error: concurrent map read and map write】问题解决
- go map fatal error:concurrent map read and map write
- Win32 WriteFile and ReadFile
- solrcloud Read and Write Side Fault Tolerance
- Python read and write
- Read and Write 单一文件
- Qt Write and Read XML File 读写XML文件
- Read and write bytes data
- JAVA ReentrantReadWriteLock AND 缓存系统
- Use the SQL Server CLR to Read and Write Text Files
- How To Read and Write XML Documents with GDataXML
- Read and write raw PCM using GStreamer
- Jackson Streaming API To Read And Write JSON
- [C++]C++小笔记之Simple File read and write-- ofstream and ifstream
- opencv xml read and write
- How To Read and Write XML Documents with GDataXML(使用GData读写XML文档)
- 关于JDK8跑项目会抛出的type mismatch between read and write
- sysbench write and read only
- Handling multiple pending socket read and write operations