文件cp功能

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>

int main(int argc,char *argv[])
{
    int src_ret,des_ret;
    char buf[128]={0};
    int src_fd,des_fd;
 
    //判断输入的参数的个数
    if(argc<3)
    {
        printf("please input src file and det file\n");
        return -1;
    }

    //打开源文件
    src_fd = open(argv[1],O_RDONLY);
    if(src_fd<0)
    {
        printf("open src file %s is fiales\n",argv[1]);
        return -2;
    }
    printf("open src file %s is sucess. fd = %d\n",argv[1],src_fd);

    //打开目的文件
    des_fd = open(argv[2],O_CREAT | O_WRONLY,0777);
    if(des_fd<0)
    {
        printf("open det file %s is fiales\n",argv[2]);
        return -3;
    }
    printf("open det file %s is sucess. wr_fd = %d\n",argv[2],des_fd);

    //将源文件拷贝到目的文件中
    while(src_ret = read(src_fd,buf,128))
    {
        if(feeor(src_ret:))
        {
            exit(0);
        }
        des_ret =  write(des_fd,buf,src_ret);
    }
    
    close(src_fd);
    close(des_fd);
    return 0;
}

 

posted @ 2016-05-20 15:28  启云  阅读(251)  评论(0编辑  收藏  举报