【Linux系统调用】使用open、perror、read、write函数来复制文本文件

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

int main(int argc,char *argv[]) {
    int srcfd = open("English.txt", O_RDONLY);
    if (srcfd == -1) {
        perror("English.txt文件没有正确打开!出错原因");
    }
    int destfd = open("copyFile.txt", O_WRONLY | O_CREAT, 0644);
    if (srcfd == -1) {
        perror("copyFile.txt文件没有正确打开!出错原因");
    }
    char buf[1024] = {0};
    int len = 0;
    while ((len = read(srcfd, buf, 1024)) > 0) {
        write(destfd, buf, len); 
    }
    close(srcfd);
    close(destfd);
    return 0;
}
posted @ 2022-06-02 17:37  OXYGEN1  阅读(62)  评论(0)    收藏  举报