码迷,mamicode.com
首页 > 其他好文 > 详细

mycp.c

时间:2017-11-29 16:09:21      阅读:788      评论:0      收藏:0      [点我收藏+]

标签:stat   include   open   write   int   fine   printf   pass   bsp   

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUFSIZE 1024
/*
   ./mycp  /etc/passwd   /home/passwd
   diff /etc/passwd  /home/passwd
*/
void copyfile(const char *s,const char *d)
{
    int fd1,fd2,rnum,wnum;
    char buf[BUFSIZE];
    fd1 = open(s,O_RDONLY);
    //if(fd1 == -1).........
    fd2 = open(d,O_WRONLY|O_CREAT|O_TRUNC,0644);
    //if(fd2 == -1)........
    while((rnum = read(fd1,buf,BUFSIZE))>0)
    {
        wnum = write(fd2,buf,rnum);
        if(wnum!=rnum)
            printf("write num less than rnum\n");
    }
    close(fd1);
    close(fd2);
}
int main(int argc,char**argv)
{
    if(argc != 3)
    {
        printf("arg num error\n");
        exit(1);
    }
    copyfile(argv[1],argv[2]);
    exit(0);
}

 

mycp.c

标签:stat   include   open   write   int   fine   printf   pass   bsp   

原文地址:http://www.cnblogs.com/yege/p/7920338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!