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

#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\n");
     exit(1);
   }
  if((out=fopen("out.pdf","w"))==NULL)//打开目标文件的文件流
   {
     printf("the new file can not open\n");
     exit(1);
   }
  while((len=fread(buffer,1,1024,in))>0)//从源文件中读取数据并放到缓冲区中,第二个参数1也可以写成sizeof(char)
   {
     fwrite(buffer,1,len,out);//将缓冲区的数据写到目标文件中
     memset(buffer,0,1024);
   }
  fclose(out);
  fclose(in);

  return 0;
}
上面的实例中,利用文件流达到了文件复制的效果,注意最后应关闭文件流。

你可能感兴趣的:(c,linux,File,buffer,语言,include)