复制一个文件并且重新命名

来源:互联网 发布:python supervisor 编辑:程序博客网 时间:2024/05/16 15:10

#include <stdio.h>

int copy_file(char *a,char *b);

int main()
{
char a[50],b[50];

printf("please input the old file namne:/n");
gets(a);

printf("please input the new file namne:/n");
gets(b);

copy_file(a,b);

return 0;

}

int copy_file(char *a,char *b)
{
FILE *fp_in,*fp_out;
int fsize;
char buffer[256];

fp_in=fopen(a,"r");

fseek(fp_in,0,SEEK_END);
fsize=ftell(fp_in);
printf("file size is %d/n",fsize);

fseek(fp_in,0,SEEK_SET);

fp_out=fopen(b,"w");

fread(buffer,fsize,1,fp_in);
fwrite(buffer,fsize,1,fp_out);

fclose(fp_in);
fclose(fp_out); 
printf("/ncopy and rename file complete/n");
return 0;

}

运行:
please input the old file namne:
in.txt
please input the new file namne:
out.txt
fsize is 9

copy and rename file complete

原创粉丝点击