WNJXYK
Thanks to the cruel world.
WNJXYKのBlog
Linux C语言 复制文件的两种方法
Linux C语言 复制文件的两种方法

使用基本IO操作:open, read, write

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

/**
 * Func: render_filename
 * @param file: File Path
 * @return: File Name
 */
const char * render_filename(const char * file){
    if (file == NULL) return NULL;
    const char * start = strrchr(file, '/');
    return (start?(start+1):file);
}

/**
 * Func: copy_file
 * @param src_file: Source File Path
 * @param dst_path: Destined Dir
 * @return: result  
 */
int copy_file(const char * src_file, const char * dst_path){
    // Open Src File
    int src_fd = open(src_file, O_RDONLY);
    if (src_fd==-1) return printf("Src File not existed.\n")*0;

    // Gen Dst File Path
    static char dst_file[1024];
    strcpy(dst_file, dst_path);
    if (dst_file[strlen(dst_file)-1]!='/') strcat(dst_file, "/");
    strcat(dst_file, render_filename(src_file));

    // Create/Rewrite Dst File
    int dst_fd = open(dst_file, O_WRONLY|O_CREAT, 0744);
    if (dst_fd==-1) return printf("Dest Path not existed or File access deined.\n")*0;

    // Copy File
    const static int MAXSIZE=1024;
    static char buff[MAXSIZE];
    static int len, res;
    while(len = read(src_fd, buff, MAXSIZE)){
        res = write(dst_fd, buff, len);
        if (len<0) return printf("Errors occured when reading.")*0;
        if (res<0) return printf("Errors occured when writing.")*0;
    }

    // Close
    close(src_fd);
    close(dst_fd);
    return 0;
}

int main(int argc, char **argv){
    // A simply test
    copy_file("/Users/wnjxyk/Documents/Linux.pdf", "/Users/wnjxyk/Documents/Linux/");
    return 0;
}

使用标准IO操作:fopen, fread, fwrite

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

/**
 * Func: render_filename
 * @param file: File Path
 * @return: File Name
 */
const char * render_filename(const char * file){
    if (file == NULL) return NULL;
    const char * start = strrchr(file, '/');
    return (start?(start+1):file);
}

/**
 * Func: copy_file
 * @param src_file: Source File Path
 * @param dst_path: Destined Dir
 * @return: result  
 */
int copy_file(const char * src_file, const char * dst_path){
    // Open Src File
    FILE *src_fp = fopen(src_file, "rb+");
    if (src_fp==NULL) return printf("Src File not existed.\n")*0;

    // Gen Dst File Path
    static char dst_file[1024];
    strcpy(dst_file, dst_path);
    if (dst_file[strlen(dst_file)-1]!='/') strcat(dst_file, "/");
    strcat(dst_file, render_filename(src_file));

    // Create/Rewrite Dst File
    FILE *dst_fp = fopen(dst_file, "wb+");
    if (dst_fp==NULL) return printf("Dest Path not existed or File access deined.\n")*0;

    // Copy File
    const static int MAXSIZE=1024;
    static char buff[MAXSIZE];
    static int len, res;
    while(len = fread(buff, 1, MAXSIZE, src_fp)){
        res = fwrite(buff, 1, len, dst_fp);
        if (len<0) return printf("Errors occured when reading.")*0;
        if (res<0) return printf("Errors occured when writing.")*0;
    }

    // Close
    fclose(src_fp);
    fclose(dst_fp);
    return 0;
}

int main(int argc, char **argv){
    // A simply test
    copy_file("/Users/wnjxyk/Documents/Linux.pdf", "/Users/wnjxyk/Documents/Matrix/");
    return 0;
}
赞赏
https://secure.gravatar.com/avatar/f83b57c055136369e9feba5d6671d6b5?s=256&r=g

WNJXYK

文章作者

一个蒟蒻

推荐文章

发表评论

textsms
account_circle
email

WNJXYKのBlog

Linux C语言 复制文件的两种方法
使用基本IO操作:open, read, write #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> /** * Func: render_filename * @para…
扫描二维码继续阅读
2018-11-05
<--! http2https -->