Linux下基于C语言流的文件复制
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);//www.bkjia.com将缓冲区的数据写到目标文件中
- memset(buffer,0,1024);
- }
- fclose(out);
- fclose(in);
- return 0;
- }
评论暂时关闭