【IO】使用read和write实现文件拷贝

#include 
#include 
#include 
#include 
#include 
#include 
 
int main(int argc, const char *argv[])
{   
    int pt=open("./1.read.c",O_RDONLY);
    int ptw=open("./write.text",O_WRONLY|O_CREAT|O_TRUNC,0664);
    if(pt<0){
        perror("open");
        return -1;
    }
    ssize_t res;
    char buf[2];                                                    
    while(1){
        bzero(buf,sizeof(buf));
        res=read(pt,buf,sizeof(buf));
        if(res==0){
            printf("读取完成\n");
            break;
        }
        else if(res<0){
            perror("read");
            break;
        }
        write(ptw,buf,sizeof(buf));
    }
    return 0;
}
                                                                    
                                                                    

你可能感兴趣的:(linux)