当前位置: 技术问答>linux和unix
求助,谁来帮我解释一下这段linux下的C程序
来源: 互联网 发布时间:2017-02-26
本文导语: #include #include #include #include #include #include #include #include #define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC) #define BUFSIZE 1024 #define size 1 int my_read(int fd,char *buf,size_t count); int my_write(int fd,char *bur,size_t count); static char ...
#include
#include
#include
#include
#include
#include
#include
#include
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define BUFSIZE 1024
#define size 1
int my_read(int fd,char *buf,size_t count);
int my_write(int fd,char *bur,size_t count);
static char read_buf[BUFSIZE];
static int read_offset=BUFSIZE;
static int read_max=BUFSIZE;
static char write_buf[BUFSIZE];
static int write_offset=0;
int main(void)
{
char infile[ ]={"test.dat"};
char outfile[ ]={"backup.dat"};
char buf[size];
int infd,outfd,count;
if((infd=open(infile,O_RDONLY))==-1)
{
printf("ERROR,OPEN READ FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
if((outfd=open(outfile,NEWFILE,0600))==-1)
{
printf("ERROR,OPEN WRITE FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
while(count=read(infd,buf,sizeof(buf))>0)
{
if(write(outfd,buf,count)!=count)
{
printf("ERROR,WRITE FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
}
if(count==-1)
{
printf("ERROR,READ FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
close(infd);
if(write_offset>0)
{
write(outfd,write_buf,write_offset);
write_offset=0;
}
close(outfd);
}
int my_read(int fd,char *buf,size_t count)
{
int i;
for(i=0; i
#include
#include
#include
#include
#include
#include
#include
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define BUFSIZE 1024
#define size 1
int my_read(int fd,char *buf,size_t count);
int my_write(int fd,char *bur,size_t count);
static char read_buf[BUFSIZE];
static int read_offset=BUFSIZE;
static int read_max=BUFSIZE;
static char write_buf[BUFSIZE];
static int write_offset=0;
int main(void)
{
char infile[ ]={"test.dat"};
char outfile[ ]={"backup.dat"};
char buf[size];
int infd,outfd,count;
if((infd=open(infile,O_RDONLY))==-1)
{
printf("ERROR,OPEN READ FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
if((outfd=open(outfile,NEWFILE,0600))==-1)
{
printf("ERROR,OPEN WRITE FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
while(count=read(infd,buf,sizeof(buf))>0)
{
if(write(outfd,buf,count)!=count)
{
printf("ERROR,WRITE FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
}
if(count==-1)
{
printf("ERROR,READ FILE FAILED: n",sys_errlist[errno]);
exit(255);
}
close(infd);
if(write_offset>0)
{
write(outfd,write_buf,write_offset);
write_offset=0;
}
close(outfd);
}
int my_read(int fd,char *buf,size_t count)
{
int i;
for(i=0; i