C语⾔复制⽂件代码⽰例
使⽤ C 语⾔标准库 <stdio.h> 中的 FILE 指针指向原⽂件和⽬标⽂件,然后调⽤函数 / 或 / 实现从原⽂件到⽬标⽂件的字节复制。
采⽤ / 函数进⾏⽂件复制的的核⼼代码
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 / ( 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[] = "";
const char dst1[] = "E:/";
const char dst2[] = "E:\\";
const char dst3[] = "E:\\";
copy_with_fgetc(src, dst1);
copy_with_fread(src, dst2);
copy_with_fread2(src, dst3);
return0;
}
View Code
注:在 Windows 系统中,如果使⽤⽬录结构,可以⽤斜杠 ‘/’ 代替反斜杠 ‘\’。如果需要使⽤反斜杠,则必须增加⼀个反斜杠进⾏转义。