C语言中,复制一个文本文件的内容到另一个文件可以通过多种方法实现。使用不同的C语言函数来复制一个文本文件的内容到另一个文件。每种方法适用于不同的场景,具体选择取决于文件大小、格式化需求以及性能考虑。

1、使用 fgetc() 逐字符复制

使用 fgetc() 从源文件逐个读取字符,并使用 fputc() 将其写入目标文件。

#include <stdio.h>

int main() {
    FILE *sourceFile, *destFile;
    char ch;

    sourceFile = fopen("source.txt", "r");
    destFile = fopen("destination.txt", "w");

    if (sourceFile == NULL || destFile == NULL) {
        perror("Error opening file");
        return 1;
    }

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

    fclose(sourceFile);
    fclose(destFile);

    printf("File copied successfully using fgetc().\n");
    return 0;
}

2、使用 fgets() 逐行复制

使用 fgets() 从源文件逐行读取内容,并使用 fputs() 将其写入目标文件。

#include <stdio.h>

int main() {
    FILE *sourceFile, *destFile;
    char buffer[256];

    sourceFile = fopen("source.txt", "r");
    destFile = fopen("destination.txt", "w");

    if (sourceFile == NULL || destFile == NULL) {
        perror("Error opening file");
        return 1;
    }

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

    fclose(sourceFile);
    fclose(destFile);

    printf("File copied successfully using fgets().\n");
    return 0;
}

3、使用 fread() 和 fwrite() 块复制

使用 fread() 从源文件中读取一定量的数据块,并使用 fwrite() 将其写入目标文件。这种方法适合大文件的复制。

#include <stdio.h>

int main() {
    FILE *sourceFile, *destFile;
    char buffer[1024];
    size_t bytesRead;

    sourceFile = fopen("source.txt", "r");
    destFile = fopen("destination.txt", "w");

    if (sourceFile == NULL || destFile == NULL) {
        perror("Error opening file");
        return 1;
    }

    while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {
        fwrite(buffer, 1, bytesRead, destFile);
    }

    fclose(sourceFile);
    fclose(destFile);

    printf("File copied successfully using fread() and fwrite().\n");
    return 0;
}

4、使用 fscanf() 和 fprintf() 复制格式化内容

使用 fscanf() 读取源文件中的格式化内容,并使用 fprintf() 将其写入目标文件。适用于处理结构化文本。

#include <stdio.h>

int main() {
    FILE *sourceFile, *destFile;
    char buffer[256];

    sourceFile = fopen("source.txt", "r");
    destFile = fopen("destination.txt", "w");

    if (sourceFile == NULL || destFile == NULL) {
        perror("Error opening file");
        return 1;
    }

    while (fscanf(sourceFile, "%s", buffer) != EOF) {
        fprintf(destFile, "%s ", buffer);
    }

    fclose(sourceFile);
    fclose(destFile);

    printf("File copied successfully using fscanf() and fprintf().\n");
    return 0;
}

5、使用低级别 I/O 函数 read() 和 write() 复制

使用 read() 从源文件读取字节数据,并使用 write() 将其写入目标文件。适用于需要更直接控制文件 I/O 的情况。

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

int main() {
    int sourceFile, destFile;
    char buffer[1024];
    ssize_t bytesRead;

    sourceFile = open("source.txt", O_RDONLY);
    destFile = open("destination.txt", O_WRONLY | O_CREAT, 0644);

    if (sourceFile == -1 || destFile == -1) {
        perror("Error opening file");
        return 1;
    }

    while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0) {
        write(destFile, buffer, bytesRead);
    }

    close(sourceFile);
    close(destFile);

    printf("File copied successfully using read() and write().\n");
    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表