Linux下基于C语言流的文件复制


  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. int main()  
  4. {  
  5.   char buffer[1024];  
  6.   FILE *in,*out;//定义两个文件流,分别用于文件的读取和写入   
  7.   int len;  
  8.   if((in=fopen("test.pdf","r"))==NULL)//打开源文件的文件流   
  9.    {  
  10.      printf("the file1 can not open\n");  
  11.      exit(1);  
  12.    }  
  13.   if((out=fopen("out.pdf","w"))==NULL)//打开目标文件的文件流   
  14.    {  
  15.      printf("the new file can not open\n");  
  16.      exit(1);  
  17.    }  
  18.   while((len=fread(buffer,1,1024,in))>0)//从源文件中读取数据并放到缓冲区中,第二个参数1也可以写成sizeof(char)   
  19.    {  
  20.      fwrite(buffer,1,len,out);//www.bkjia.com将缓冲区的数据写到目标文件中   
  21.      memset(buffer,0,1024);  
  22.    }  
  23.   fclose(out);  
  24.   fclose(in);  
  25.   
  26.   return 0;  
  27. }  
上面的实例中,利用文件流达到了文件复制的效果,注意最后应关闭文件流。

相关内容