|
/* Copy a file "file.bin" on the drive 1 to drive 0 */ int main (void) { FATFS fs[2]; /* Work area (file system object) for logical drives */ FIL fsrc, fdst; /* File objects */ BYTE buffer[4096]; /* File copy buffer */ FRESULT fr; /* FatFs function common result code */ UINT br, bw; /* File read/write count */ /* Register work area for each logical drive */ f_mount(&fs[0], "0:", 0); f_mount(&fs[1], "1:", 0); /* Open source file on the drive 1 */ fr = f_open(&fsrc, "1:file.bin", FA_READ); if (fr) return (int)fr; /* Create destination file on the drive 0 */ fr = f_open(&fdst, "0:file.bin", FA_WRITE | FA_CREATE_ALWAYS); if (fr) return (int)fr; /* Copy source to destination */ for (;;) { fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of source file */ if (fr || br == 0) break; /* error or eof */ fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ if (fr || bw < br) break; /* error or disk full */ } /* Close open files */ f_close(&fsrc); f_close(&fdst); /* Unregister work area prior to discard it */ f_mount(NULL, "0:", 0); f_mount(NULL, "1:", 0); return (int)fr; }
|
|
/* ドライブ1のファイル "file.bin" をドライブ0へコピー */ int main (void) { FATFS fs[2]; /* 論理ドライブのワークエリア(ファイル システム オブジェクト) */ FIL fsrc, fdst; /* ファイル オブジェクト */ BYTE buffer[4096]; /* File copy buffer */ FRESULT fr; /* FatFs function common result code */ UINT br, bw; /* File R/W count */ /* ドライブ0,1にワーク エリアを与える */ f_mount(&fs[0], "0:", 0); f_mount(&fs[1], "1:", 0); /* ドライブ1のコピー元ファイルを開く */ res = f_open(&fsrc, "1:file.dat", FA_OPEN_EXISTING | FA_READ); if (fr) return (int)fr; /* ドライブ0にコピー先ファイルを作成する */ res = f_open(&fdst, "0:file.dat", FA_CREATE_ALWAYS | FA_WRITE); if (fr) return (int)fr; /* コピー元からコピー先にデータ転送する */ for (;;) { res = f_read(&fsrc, buffer, sizeof buffer, &br); /* コピー元からから読み出す */ if (res || br == 0) break; /* エラーかファイル終端 */ res = f_write(&fdst, buffer, br, &bw); /* それをコピー先に書き込む */ if (res || bw < br) break; /* エラーかディスク満杯 */ } /* 全てのファイルを閉じる */ f_close(&fsrc); f_close(&fdst); /* ワーク エリアを開放する */ f_mount(NULL, "0:", 0); f_mount(NULL, "1:", 0); return (int)fr; }
|