萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

C 語言檔案內容交換:兩個文字檔案資料互換完整實作

需求描述

將兩個文字檔案的內容完整交換:jack.txt ↔ retchie.txt。

標準實作(雙緩衝區法)

#include 
#include 
#include 

#define MAX_LINE 1024

int main(void)
{
    const char* file1 = "e:/jack.txt";
    const char* file2 = "e:/retchie.txt";

    char *buf1 = NULL, *buf2 = NULL;
    size_t len1 = 0, len2 = 0;
    FILE *fp1 = NULL, *fp2 = NULL;

    // ----- 1. 讀取檔案 1 -----
    fp1 = fopen(file1, "rb"); // 二進位模式避免換行符轉換
    if (!fp1) { perror("開啟 jack.txt 失敗"); goto CLEANUP; }
    fseek(fp1, 0, SEEK_END);
    len1 = ftell(fp1);
    fseek(fp1, 0, SEEK_SET);
    buf1 = (char*)malloc(len1 + 1);
    if (!buf1) { perror("記憶體配置失敗"); goto CLEANUP; }
    fread(buf1, 1, len1, fp1);
    buf1[len1] = '\0';
    fclose(fp1); fp1 = NULL;

    // ----- 2. 讀取檔案 2 -----
    fp2 = fopen(file2, "rb");
    if (!fp2) { perror("開啟 retchie.txt 失敗"); goto CLEANUP; }
    fseek(fp2, 0, SEEK_END);
    len2 = ftell(fp2);
    fseek(fp2, 0, SEEK_SET);
    buf2 = (char*)malloc(len2 + 1);
    if (!buf2) { perror("記憶體配置失敗"); goto CLEANUP; }
    fread(buf2, 1, len2, fp2);
    buf2[len2] = '\0';
    fclose(fp2); fp2 = NULL;

    // ----- 3. 寫回交換 -----
    fp1 = fopen(file1, "wb");
    if (!fp1) { perror("寫入 jack.txt 失敗"); goto CLEANUP; }
    fwrite(buf2, 1, len2, fp1);
    fclose(fp1); fp1 = NULL;

    fp2 = fopen(file2, "wb");
    if (!fp2) { perror("寫入 retchie.txt 失敗"); goto CLEANUP; }
    fwrite(buf1, 1, len1, fp2);
    fclose(fp2); fp2 = NULL;

    printf("檔案內容交換完成。\n");

CLEANUP:
    free(buf1);
    free(buf2);
    if (fp1) fclose(fp1);
    if (fp2) fclose(fp2);
    return 0;
}

進階:大檔案分塊交換(避免記憶體爆炸)

#define CHUNK_SIZE (64 * 1024) // 64KB

void SwapFilesChunked(const char* f1, const char* f2)
{
    FILE *fp1 = fopen(f1, "rb"), *fp2 = fopen(f2, "rb");
    if (!fp1 || !fp2) { perror("開啟失敗"); goto END; }

    // 取得檔案大小
    fseek(fp1, 0, SEEK_END); long sz1 = ftell(fp1); fseek(fp1, 0, SEEK_SET);
    fseek(fp2, 0, SEEK_END); long sz2 = ftell(fp2); fseek(fp2, 0, SEEK_SET);

    // 建立暫存檔儲存較大檔案
    char tmpName[L_tmpnam]; tmpnam(tmpName);
    FILE *tmp = fopen(tmpName, "wb");
    if (!tmp) { perror("建立暫存檔失敗"); goto END; }

    // 將較大檔案複製到暫存檔
    FILE *larger = (sz1 >= sz2) ? fp1 : fp2;
    long largerSz = (sz1 >= sz2) ? sz1 : sz2;
    char* chunk = (char*)malloc(CHUNK_SIZE);
    while (largerSz > 0) {
        size_t n = (largerSz > CHUNK_SIZE) ? CHUNK_SIZE : largerSz;
        fread(chunk, 1, n, larger);
        fwrite(chunk, 1, n, tmp);
        largerSz -= n;
    }
    fclose(tmp); free(chunk);

    // 關閉原檔,重新以 wb 寫入交換
    fclose(fp1); fclose(fp2);
    fp1 = fopen(f1, "wb"); fp2 = fopen(f2, "wb");
    if (!fp1 || !fp2) { perror("重新開啟寫入失敗"); goto END; }

    // 較小檔案直接寫入較大檔案原位置
    FILE *smaller = (sz1 < sz2) ? fp1 : fp2;
    long smallerSz = (sz1 < sz2) ? sz1 : sz2;
    chunk = (char*)malloc(CHUNK_SIZE);
    rewind((sz1 < sz2) ? fp2 : fp1); // 原較大檔案指標已在開頭
    while (smallerSz > 0) {
        size_t n = (smallerSz > CHUNK_SIZE) ? CHUNK_SIZE : smallerSz;
        fread(chunk, 1, n, (sz1 < sz2) ? fp2 : fp1);
        fwrite(chunk, 1, n, smaller);
        smallerSz -= n;
    }
    free(chunk);

    // 暫存檔內容寫入較小檔案原位置
    tmp = fopen(tmpName, "rb");
    chunk = (char*)malloc(CHUNK_SIZE);
    while ((n = fread(chunk, 1, CHUNK_SIZE, tmp)) > 0)
        fwrite(chunk, 1, n, (sz1 >= sz2) ? fp2 : fp1);
    free(chunk); fclose(tmp);
    remove(tmpName);

    printf("大檔案分塊交換完成。\n");

END:
    if (fp1) fclose(fp1);
    if (fp2) fclose(fp2);
}

系統級最優解:rename 原子交換(POSIX/Windows 皆支援)

#include 
#include 

int SwapFilesAtomic(const char* f1, const char* f2)
{
    char tmp[L_tmpnam];
    tmpnam(tmp); // 產生唯一暫存檔名

    if (rename(f1, tmp) != 0) { perror("rename f1->tmp"); return -1; }
    if (rename(f2, f1) != 0) { perror("rename f2->f1"); rename(tmp, f1); return -1; }
    if (rename(tmp, f2) != 0) { perror("rename tmp->f2"); rename(f1, f2); rename(tmp, f1); return -1; }
    return 0;
}
優勢:零資料拷貝、原子性(同檔案系統)、瞬間完成、支援任意大小檔案、無記憶體壓力。**這是生產環境首選方案。**

方案對比總結

方案記憶體速度原子性適用場景
全讀記憶體交換O(檔案大小)快(小檔)否小檔案 (< 100MB)、教學示範
分塊 + 暫存檔O(CHUNK_SIZE)中否大檔案、記憶體受限
rename 原子交換O(1)極快是(同分區)所有生產場景首選

結語

檔案內容交換看似簡單,**rename 三步驟原子交換**才是專業級解法:零拷貝、原子、無大小限制。教學用雙緩衝區法理解邏輯,生產碼請直接用 rename。

copyright © 萬盛學電腦網 all rights reserved