C语言中,文件复制是指将一个文件的内容复制到另一个文件中。需要使用文件输入/输出(I/O)函数来完成。不同的方法来实现文件复制,每种方法都有其适用的场景和特点。

1、使用标准库函数 fgetc 和 fputc

使用标准库函数逐个字符地读写文件。

#include <stdio.h>

void copyFile(const char *src, const char *dest) {
    FILE *sourceFile = fopen(src, "r");
    FILE *destFile = fopen(dest, "w");
    if (sourceFile == NULL || destFile == NULL) {
        printf("Error opening files.\n");
        return;
    }

    int ch;
    while ((ch = fgetc(sourceFile)) != EOF) {
        fputc(ch, destFile);
    }

    fclose(sourceFile);
    fclose(destFile);
}

int main() {
    FILE *fd = fopen("source.txt","w+");
    if(fd != NULL){
        printf("%s 源文件创建\n","source.txt");
    }
    copyFile("source.txt", "destination.txt");
    FILE *fd1 = fopen("destination.txt","r");
    if(fd1 != NULL){
        printf("%s 是复制文件\n","destination.txt");
    }
    return 0;
}

2、使用 fread 和 fwrite

使用标准库函数一次读写多个字符。

#include <stdio.h>

void copyFile(const char *src, const char *dest) {
    FILE *sourceFile = fopen(src, "rb");
    FILE *destFile = fopen(dest, "wb");
    if (sourceFile == NULL || destFile == NULL) {
        printf("Error opening files.\n");
        return;
    }

    char buffer[1024];
    size_t bytesRead;
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {
        fwrite(buffer, 1, bytesRead, destFile);
    }

    fclose(sourceFile);
    fclose(destFile);
}

int main() {
    FILE *fd = fopen("source.txt","w+");
    if(fd != NULL){
        printf("%s 源文件创建\n","source.txt");
    }
    copyFile("source.txt", "destination.txt");
    FILE *fd1 = fopen("destination.txt","r");
    if(fd1 != NULL){
        printf("%s 是复制文件\n","destination.txt");
    }
    return 0;
}

3、使用低级文件I/O函数 read 和 write

这种方法使用POSIX标准的低级文件I/O函数进行文件复制。

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

void copyFile(const char *src, const char *dest) {
    int sourceFile = open(src, O_RDONLY);
    int destFile = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (sourceFile == -1 || destFile == -1) {
        printf("Error opening files.\n");
        return;
    }

    char buffer[1024];
    ssize_t bytesRead;
    while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0) {
        write(destFile, buffer, bytesRead);
    }

    close(sourceFile);
    close(destFile);
}

int main() {
    FILE *fd = fopen("source.txt","w+");
    if(fd != NULL){
        printf("%s 源文件创建\n","source.txt");
    }
    copyFile("source.txt", "destination.txt");
    FILE *fd1 = fopen("destination.txt","r");
    if(fd1 != NULL){
        printf("%s 是复制文件\n","destination.txt");
    }
    return 0;
}

4、使用标准库函数 fgets 和 fputs

这种方法逐行读取和写入文件,适用于文本文件。

#include <stdio.h>

void copyFile(const char *src, const char *dest) {
    FILE *sourceFile = fopen(src, "r");
    FILE *destFile = fopen(dest, "w");
    if (sourceFile == NULL || destFile == NULL) {
        printf("Error opening files.\n");
        return;
    }

    char buffer[1024];
    while (fgets(buffer, sizeof(buffer), sourceFile) != NULL) {
        fputs(buffer, destFile);
    }

    fclose(sourceFile);
    fclose(destFile);
}

int main() {
    FILE *fd = fopen("source.txt","w+");
    if(fd != NULL){
        printf("%s 源文件创建\n","source.txt");
    }
    copyFile("source.txt", "destination.txt");
    FILE *fd1 = fopen("destination.txt","r");
    if(fd1 != NULL){
        printf("%s 是复制文件\n","destination.txt");
    }
    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表