c语言编写程序,完成把一个文件的内容复制到另一个文件中去。源文件的名字从键盘输入,目的文件的名字也
答案:2 悬赏:70 手机版
解决时间 2021-03-26 19:53
- 提问者网友:太高姿态
- 2021-03-26 03:11
c语言编写程序,完成把一个文件的内容复制到另一个文件中去。源文件的名字从键盘输入,目的文件的名字也
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-03-26 04:31
#include
int Copy_File(char* in_path,char* out_path)
{
FILE* in=NULL;
FILE* out=NULL;
if((in=fopen(in_path,"rb"))==NULL)
{
printf("无法打开源文件!\n");
return(1);
}
if((out=fopen(out_path,"wb+"))==NULL)
{
printf("无法创建目标文件!\n");
fclose(in);
return(1);
}
char data;
while(!feof(in))
{
if(1==fread(&data,sizeof(char),1,in))
fwrite(&data,sizeof(char),1,out);
}
fclose(in);
fclose(out);
return(0);
}
int main()
{
char in_path[256];
char out_path[256];
printf("请输入源文件的路径: ");
scanf("%s",in_path);
printf("请输入目的文件的路径: ");
scanf("%s",out_path);
if(Copy_File(in_path,out_path))
{
printf("拷贝失败!");
}else
printf("拷贝成功!");
return(0);
}
int Copy_File(char* in_path,char* out_path)
{
FILE* in=NULL;
FILE* out=NULL;
if((in=fopen(in_path,"rb"))==NULL)
{
printf("无法打开源文件!\n");
return(1);
}
if((out=fopen(out_path,"wb+"))==NULL)
{
printf("无法创建目标文件!\n");
fclose(in);
return(1);
}
char data;
while(!feof(in))
{
if(1==fread(&data,sizeof(char),1,in))
fwrite(&data,sizeof(char),1,out);
}
fclose(in);
fclose(out);
return(0);
}
int main()
{
char in_path[256];
char out_path[256];
printf("请输入源文件的路径: ");
scanf("%s",in_path);
printf("请输入目的文件的路径: ");
scanf("%s",out_path);
if(Copy_File(in_path,out_path))
{
printf("拷贝失败!");
}else
printf("拷贝成功!");
return(0);
}
全部回答
- 1楼网友:一把行者刀
- 2021-03-26 05:19
#include
#include
#include
#ifdef BUFSIZ
#undef BUFSIZ
#define BUFSIZ 4096
#endif
int main(int argc,char **argv)
{
char buf[BUFSIZ];
int msglen;
if(argc!=3||strcmp(argv[1],argv[2])==0)
{
fprintf(stderr,"********************************\n\n");
fprintf(stderr,"Please usage:%s source_file destination_file\nAnd source_file is different from destination_file\n\n",argv[0]);
fprintf(stderr,"********************************\n");
exit(0);
}
FILE *fp_src,*fp_des;
if((fp_src=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"open %s failed!\n",argv[1]);
exit(1);
}
if((fp_des=fopen(argv[2],"w"))==NULL)
{
fprintf(stderr,"open/create %s failed!\n",argv[2]);
exit(2);
}
while(fgets(buf,BUFSIZ,fp_src)!=NULL)
{
if(fputs(buf,fp_des)==EOF)
{
fprintf(stderr,"copy %s to %s failed!\n",argv[1],argv[2]);
exit(3);
}
}
printf("copy %s to %s successful!\n",argv[1],argv[2]);
return 0;
}
#include
#include
#ifdef BUFSIZ
#undef BUFSIZ
#define BUFSIZ 4096
#endif
int main(int argc,char **argv)
{
char buf[BUFSIZ];
int msglen;
if(argc!=3||strcmp(argv[1],argv[2])==0)
{
fprintf(stderr,"********************************\n\n");
fprintf(stderr,"Please usage:%s source_file destination_file\nAnd source_file is different from destination_file\n\n",argv[0]);
fprintf(stderr,"********************************\n");
exit(0);
}
FILE *fp_src,*fp_des;
if((fp_src=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"open %s failed!\n",argv[1]);
exit(1);
}
if((fp_des=fopen(argv[2],"w"))==NULL)
{
fprintf(stderr,"open/create %s failed!\n",argv[2]);
exit(2);
}
while(fgets(buf,BUFSIZ,fp_src)!=NULL)
{
if(fputs(buf,fp_des)==EOF)
{
fprintf(stderr,"copy %s to %s failed!\n",argv[1],argv[2]);
exit(3);
}
}
printf("copy %s to %s successful!\n",argv[1],argv[2]);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯