C语言复制文件代码示例
使用 C 语言标准库 <stdio.h> 中的 FILE 指针指向原文件和目标文件,然后调用函数 fgetc/fputc 或 fread/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;
}注:在 Windows 系统中,如果使用目录结构,可以用斜杠 ‘/’ 代替反斜杠 ‘\’。如果需要使用反斜杠,则必须增加一个反斜杠进行转义。
相关推荐
拉斯厄尔高福 2020-11-04
嵌入式资讯精选 2020-10-15
zhaochen00 2020-10-13
penkgao 2020-10-13
wanshiyingg 2020-09-29
Mars的自语 2020-09-27
shenwenjie 2020-09-24
一个逗逗 2020-09-22
flycony 2020-09-13
zhaochen00 2020-08-20
Biao 2020-08-20
qingsongzdq 2020-08-19
penkgao 2020-08-17
cetrolchen 2020-08-14
GuoSir 2020-08-07