- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char buffer[1024];
- FILE *in,*out;//定义两个文件流,分别用于文件的读取和写入
- int len;
- if((in=fopen("test.pdf","r"))==NULL)//打开源文件的文件流
- {
- printf("the file1 can not open
");
- exit(1);
- }
- if((out=fopen("out.pdf","w"))==NULL)//打开目标文件的文件流
- {
- printf("the new file can not open
");
- exit(1);
- }
- while((len=fread(buffer,1,1024,in))>0)//从源文件中读取数据并放到缓冲区中,第二个参数1也可以写成sizeof(char)
- {
- fwrite(buffer,1,len,out);//www.linuxidc.com将缓冲区的数据写到目标文件中
- memset(buffer,0,1024);
- }
- fclose(out);
- fclose(in);
-
- return 0;
- }
上面的实例中,利用文件流达到了文件复制的效果,注意最后应关闭文件流。