C語言復制文件代碼示例


使用 C 語言標准庫 <stdio.h> 中的 FILE 指針指向原文件和目標文件,然后調用函數 fgetc/fputcfread/fwrite 實現從原文件到目標文件的字節復制。

采用 fgetc/fputc 函數進行文件復制的的核心代碼

int val = 0; while ((val = fgetc(fpbr)) != EOF) fputc(val, fpbw);

注:
1)EOF宏,表示文件尾(End Of File),定義在 <stdio.h> 頭文件中,其值為 -1;
2)雖然 fgetc/fputc 函數的功能是從文件流中讀/寫一個字符,實際上字符使用的是 int 整型,不是 char 類型。如果使用 char 類型,可能會導致文件復制不全;

采用 fread/fwrite  函數進行文件復制的核心代碼

示例代碼一

char ch; while (fread(&ch, sizeof(char), 1, fpbr) != 0) fwrite(&ch, sizeof(char), 1, fpbw);

示例代碼二

size_t len = 0; char buffer[BUFSIZ] = {'\0'};  // BUFSIZ macro defined in <stdio.h>
while ((len = fread(buffer, sizeof(char), BUFSIZ, fpbr)) > 0) fwrite(buffer, sizeof(char), len, fpbw);

注:
1)BUFSIZ 是定義在頭文件 <stdio.h> 中的宏,其值為 512;
2)fread/fwrite 函數的功能是從文件流中讀/寫塊數據,其函數原型均為 size_t fread/fwrite ( void * ptr, size_t size, size_t count, FILE * stream ); 其中 ptr 為讀寫所用的元素數組指針; size 為用於讀寫的每個元素大小,size_t 類型為無符號整型(unsigned integer type);count 為元素數組的大小,每個元素占用 size 個字節;stream 為指向文件的 FILE 指針。

完整代碼

#include <stdio.h>
#include <stdlib.h>  // exit()

void copy_with_fgetc(const char src[], const char dst[])
{
    FILE *fpbr, *fpbw;

    // Try to open source file
    fpbr = fopen(src, "rb");
    if (fpbr == NULL) {
        printf("Error for opening source file %s!\n", src);
        exit(1);
    }

    // Try to open destination file
    fpbw = fopen(dst, "wb");
    if (fpbr == NULL) {
        printf("Error for opening destination file %s!\n", dst);
        exit(1);
    }

    // Copy file with fgetc() and fputc()
    int val = 0;
    while ((val = fgetc(fpbr)) != EOF)
        fputc(val, fpbw);

    printf("Copy file successfully!\n");

    fclose(fpbr);
    fclose(fpbw);
}

void copy_with_fread(const char src[], const char dst[])
{
    FILE *fpbr, *fpbw;

    // Try to open source file
    fpbr = fopen(src, "rb");
    if (fpbr == NULL) {
        printf("Error for opening source file %s!\n", src);
        exit(1);
    }

    // Try to open destination file
    fpbw = fopen(dst, "wb");
    if (fpbr == NULL) {
        printf("Error for opening destination file %s!\n", dst);
        exit(1);
    }

    // Copy file with fread() and fwrite()
    char ch;
    while (fread(&ch, sizeof(char), 1, fpbr) != 0)
        fwrite(&ch, sizeof(char), 1, fpbw);

    printf("Copy file successfully!\n");

    fclose(fpbr);
    fclose(fpbw);
}

void copy_with_fread2(const char src[], const char dst[])
{
    FILE *fpbr, *fpbw;

    // Try to open source file
    fpbr = fopen(src, "rb");
    if (fpbr == NULL) {
        printf("Error for opening source file %s!\n", src);
        exit(1);
    }

    // Try to open destination file
    fpbw = fopen(dst, "wb");
    if (fpbr == NULL) {
        printf("Error for opening destination file %s!\n", dst);
        exit(1);
    }

    // Copy file with buffer fread() and fwrite()
    size_t len = 0;
    char buffer[BUFSIZ] = {'\0'};  // BUFSIZ macro defined in <stdio.h>
    while ((len = fread(buffer, sizeof(char), BUFSIZ, fpbr)) > 0)
        fwrite(buffer, sizeof(char), len, fpbw);

    printf("Copy file successfully!\n");

    fclose(fpbr);
    fclose(fpbw);
}

int main()
{
    const char src[] = "HelloWorld.exe";
    const char dst1[] = "E:/HelloWorld1.exe";
    const char dst2[] = "E:\\HelloWorld2.exe";
    const char dst3[] = "E:\\HelloWorld3.exe";

    copy_with_fgetc(src, dst1);
    copy_with_fread(src, dst2);
    copy_with_fread2(src, dst3);

    return 0;
}
View Code

 注:在 Windows 系統中,如果使用目錄結構,可以用斜杠 ‘/’ 代替反斜杠 ‘\’。如果需要使用反斜杠,則必須增加一個反斜杠進行轉義。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM