操作系统实验之文件拷贝

实验要求

使用系统调用(open, read, write,close等函数)编写文件拷贝程序.

实验环境

LinuxMax OS X

实验代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char** argv){
    if(argc != 3){
        fprintf(stderr, "You must need a source file and destination file\n");
        exit(0);
    }
    int so = open(argv[1], O_RDONLY|O_CREAT, 0644);
    int de = open(argv[2], O_WRONLY|O_CREAT, 0644);
    char c;
    while(read(so, &c, 1)){
        write(de, &c, 1);
    }
    close(so);
    close(de);
    printf("copy over\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33528164/article/details/80205104