c语言 文件拷贝_拷贝文件公司会发现吗[通俗易懂]

c语言 文件拷贝_拷贝文件公司会发现吗[通俗易懂]利用文件IO的方式,实现2个文件的拷贝intcopy_by_fileIO(constchar*dest_file_name,constchar*src_file_name){//OKintfd

利用文件IO的方式,实现2个文件的拷贝

int copy_by_fileIO(const char *dest_file_name, const char *src_file_name) { 
   //OK
    int fd1 = open(dest_file_name,O_WRONLY|O_CREAT|O_APPEND,0766);
    if(fd1 == -1) { 
   
        perror("fd1:");
        return -1;
    }
    int fd2 = open(src_file_name,O_RDONLY|S_IROTH);
    if(fd2 == -1) { 
   
        perror("fd2:");
        return -1;
    }
    char *buffer = (char *)malloc(2 * sizeof(char));
    int ans = 0;
    int k;
    do{ 
   
        memset(buffer,0,sizeof(buffer));
        k = read(fd2,buffer,1);
        if(k <= 0) break;
        write(fd1,buffer,1);
        //printf("k = %d\tbuffer = %s\n",k,buffer);
        ans++;
    }while(k > 0);
    close(fd1);
    close(fd2);
    return ans;
}

利用标准IO的字符IO,实现2个文件的字节拷贝

int copy_by_char(const char *dest_file_name, const char *src_file_name) { 
   //OK
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    char buffer = fgetc(fp2);
    int cnt = 0;
    while(!feof(fp2)) { 
   
        cnt++;
        fputc(buffer,fp1);
        buffer = fgetc(fp2);
    }
    fclose(fp1);
    fclose(fp2);
    return cnt;
}

利用标准IO的行IO,实现2个文件的按行拷贝

int copy_by_line(const char *dest_file_name, const char *src_file_name) { 
    //ok
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    char * buffer = (char *)malloc(sizeof(char) * 101);
    int cnt = 0;
    while(fgets(buffer,100,fp2)) { 
   
        fputs(buffer,fp1);
        cnt++;
    }
    free(buffer);
    fclose(fp1);
    fclose(fp2);
    return cnt;//返回的是读取的行数
}

利用标准IO的块IO,实现2个文件的块拷贝

int copy_by_block(const char *dest_file_name, const char *src_file_name) { 
   //ok
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    void *buffer = (void *)malloc(2);
    int cnt = 0;
    while(1) { 
   
        int op = fread(buffer,1,1,fp2);
        if(op <= 0) break;
        fwrite(buffer,1,1,fp1);
        cnt++;
    }
    free(buffer);
    fclose(fp1);
    fclose(fp2);
    return cnt;
}

汇总

//
// Created by Mangata on 2021/10/5.
/* * 需求描述:分别使用文件IO,标准字符、标准行、标准块IO实现文本文件的拷贝功能 * 思考,如果是普通的图片、视频文件,上述拷贝哪些可以用,哪些不可以用 * */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* * 利用文件IO的方式,实现2个文件的拷贝 * */
int copy_by_fileIO(const char *dest_file_name, const char *src_file_name) { 
   //OK
    int fd1 = open(dest_file_name,O_WRONLY|O_CREAT|O_APPEND,0766);
    if(fd1 == -1) { 
   
        perror("fd1:");
        return -1;
    }
    int fd2 = open(src_file_name,O_RDONLY|S_IROTH);
    if(fd2 == -1) { 
   
        perror("fd2:");
        return -1;
    }
    char *buffer = (char *)malloc(2 * sizeof(char));
    int ans = 0;
    int k;
    do{ 
   
        memset(buffer,0,sizeof(buffer));
        k = read(fd2,buffer,1);
        if(k <= 0) break;
        write(fd1,buffer,1);
        //printf("k = %d\tbuffer = %s\n",k,buffer);
        ans++;
    }while(k > 0);
    close(fd1);
    close(fd2);
    return ans;
}

/* * 利用标准IO的字符IO,实现2个文件的字节拷贝 * */
int copy_by_char(const char *dest_file_name, const char *src_file_name) { 
   //OK
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    char buffer = fgetc(fp2);
    int cnt = 0;
    while(!feof(fp2)) { 
   
        cnt++;
        fputc(buffer,fp1);
        buffer = fgetc(fp2);
    }
    fclose(fp1);
    fclose(fp2);
    return cnt;
}

/* * 利用标准IO的行IO,实现2个文件的按行拷贝 * */
int copy_by_line(const char *dest_file_name, const char *src_file_name) { 
    //ok
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    char * buffer = (char *)malloc(sizeof(char) * 101);
    int cnt = 0;
    while(fgets(buffer,100,fp2)) { 
   
        fputs(buffer,fp1);
        cnt++;
    }
    free(buffer);
    fclose(fp1);
    fclose(fp2);
    return cnt;//返回的是读取的行数
}

/* * 利用标准IO的块IO,实现2个文件的块拷贝 * */
int copy_by_block(const char *dest_file_name, const char *src_file_name) { 
   //ok
    FILE *fp1 = fopen(dest_file_name,"w");
    FILE *fp2 = fopen(src_file_name,"r");
    if(fp1 == NULL) { 
   
        perror("fp1:");
        return -1;
    }
    if(fp2 == NULL) { 
   
        perror("fp2:");
        return -1;
    }
    void *buffer = (void *)malloc(2);
    int cnt = 0;
    while(1) { 
   
        int op = fread(buffer,1,1,fp2);
        if(op <= 0) break;
        fwrite(buffer,1,1,fp1);
        cnt++;
    }
    free(buffer);
    fclose(fp1);
    fclose(fp2);
    return cnt;
}

int main() { 
   
    char *src_file_path = "/etc/profile";
    char *dest_file_path = "/tmp/test_profile";
    int cnt = copy_by_fileIO(dest_file_path,src_file_path);
    printf("cnt = %d\n",cnt);
    return 0;
}





在这里插入图片描述

今天的文章c语言 文件拷贝_拷贝文件公司会发现吗[通俗易懂]分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/65245.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注