zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    Oracle数据库基础
    2016-08-08二期模拟考试
    易买网-登入
    常量接口模式
    反射
    Hhibernate延迟加载
    URL和URI的区别和联系
    Socket编程
    ArrayList如何实现线程安全
    移位运算符
  • 原文地址:https://www.cnblogs.com/powercool/p/12915419.html
Copyright © 2011-2022 走看看