【linux系统编程】使用read(),write()函数写个简单的cp复制

使用read(),write()函数,写一个简单的复制

 

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

int main(int argc,char *argv[])
{
    char buf[1024];
    int n=0;
    
    int fd1=open(argv[1],O_RDONLY);
    if(fd1 == -1){
        perror("open argv1 error");
        exit(1);
    }
    int fd2=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0664);
    
    if(fd2 == -1){
        perror("open argv2 error");
        exit(1);
    }
    while((n=read(fd1,buf,1024)) !=0)
    {
        if(n<0){
            perror("read error");
            break;
        }
        write(fd2,buf,n);
    }
    close(fd1);
    close(fd2);
    return 0;

}

 

 

 

使用make进行编译

src = $(wildcard *.c)
target = $(patsubst %.c,%,$(src))

ALL:$(target)

%:%.c

    gcc $< -o $@
clean:

    -rm -rf $(target)

.PHONY: clean ALL

 

posted @ 2020-05-19 10:24  老年新手工程师  阅读(498)  评论(0编辑  收藏  举报