C语言编程实现:将一个文件的内容复制到另一个文件。(详细点的,考试用。)谢谢!
答案:6 悬赏:10 手机版
解决时间 2021-03-23 02:53
- 提问者网友:不爱我么
- 2021-03-22 21:54
C语言编程实现:将一个文件的内容复制到另一个文件。(详细点的,考试用。)谢谢!
最佳答案
- 五星知识达人网友:行路难
- 2021-03-22 23:04
#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)
{
}
FILE *fp_src,*fp_des;
if((fp_src=fopen(argv[1],"r"))==NULL)
{
}
if((fp_des=fopen(argv[2],"w"))==NULL)
{
}
while(fgets(buf,BUFSIZ,fp_src)!=NULL)
{
if(fputs(buf,fp_des)==EOF)
{
}
}
}
#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,"********************************
");
fprintf(stderr,"Please usage:%s source_file destination_file
And source_file is different from destination_file
",argv[0]);
fprintf(stderr,"********************************
");
}
FILE *fp_src,*fp_des;
if((fp_src=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"open %s failed!
",argv[1]);
}
if((fp_des=fopen(argv[2],"w"))==NULL)
{
fprintf(stderr,"open/create %s failed!
",argv[2]);
}
while(fgets(buf,BUFSIZ,fp_src)!=NULL)
{
if(fputs(buf,fp_des)==EOF)
{
fprintf(stderr,"copy %s to %s failed!
",argv[1],argv[2]);
}
}
printf("copy %s to %s successful!
",argv[1],argv[2]);
}
全部回答
- 1楼网友:傲气稳了全场
- 2021-03-23 00:54
很多年前的事儿了,忘记了。
- 2楼网友:你哪知我潦倒为你
- 2021-03-23 00:40
打开两个文件,从一个文件读数据,写入到另一个文件,例如: //------{ FILE *fp1,fp2; char c; fp1=fopen("dat.txt","r"); /*打开
- 3楼网友:白昼之月
- 2021-03-22 23:57
方法1
#include
#include
void main()
{
char filename[100],filename1[50],filename2[50];
printf("请输入要读的文件名:");
scanf("%s",filename1);
printf("请输入要写的文件名:");
scanf("%s",filename2);
sprintf(filename,"copy %s %s /y>nul",filename1,filename2);
system(filename);
}
方法2
#include
void main()
{
FILE *source,*object;
size_t readlen;
char filename1[50],filename2[50];
char diskbuffer[8192];
printf("请输入要读的文件名:");
scanf("%s",filename1);
printf("请输入要写的文件名:");
scanf("%s",filename2);
source=fopen(filename1,"rb");
if (source==NULL)
{
fclose(source);
return;
}
object=fopen(filename2,"wb");
do
{
readlen=fread(diskbuffer,1,8192,source);
fwrite(diskbuffer,1,readlen,object);
}while(!feof(source));
fclose(object);
fclose(source);
}
#include
#include
void main()
{
char filename[100],filename1[50],filename2[50];
printf("请输入要读的文件名:");
scanf("%s",filename1);
printf("请输入要写的文件名:");
scanf("%s",filename2);
sprintf(filename,"copy %s %s /y>nul",filename1,filename2);
system(filename);
}
方法2
#include
void main()
{
FILE *source,*object;
size_t readlen;
char filename1[50],filename2[50];
char diskbuffer[8192];
printf("请输入要读的文件名:");
scanf("%s",filename1);
printf("请输入要写的文件名:");
scanf("%s",filename2);
source=fopen(filename1,"rb");
if (source==NULL)
{
fclose(source);
return;
}
object=fopen(filename2,"wb");
do
{
readlen=fread(diskbuffer,1,8192,source);
fwrite(diskbuffer,1,readlen,object);
}while(!feof(source));
fclose(object);
fclose(source);
}
- 4楼网友:怙棘
- 2021-03-22 23:19
//程序完成的操作:将D盘下 1.txt 文件中内容拷贝至 2.txt 文件中
#include
#include
void FileCopy(FILE *,FILE *);//拷贝子程序申明
void main(void)
{
FILE *fpin, *fpout;
if((fpin = fopen("D:\1.txt","rb")) == NULL)
{
printf("1 can't open file! /n");//文件打开失败打印输出
return;
}
if((fpout = fopen("D:\2.txt","wb")) == NULL)
{
printf("2 can't open file! /n");
return;
}
FileCopy(fpin,fpout);
fclose(fpin);
fclose(fpout);
fpin = NULL;
fpout = NULL;
printf("~~~~~~~~~~~~~~~~~~");
return;
}
void FileCopy(FILE *fpin, FILE *fpout)
{
char ch;
ch = getc(fpin);
while(!feof(fpin))
{
putc(ch,fpout);
ch = getc(fpin);
}
}
求加分 哈哈
#include
#include
void FileCopy(FILE *,FILE *);//拷贝子程序申明
void main(void)
{
FILE *fpin, *fpout;
if((fpin = fopen("D:\1.txt","rb")) == NULL)
{
printf("1 can't open file! /n");//文件打开失败打印输出
return;
}
if((fpout = fopen("D:\2.txt","wb")) == NULL)
{
printf("2 can't open file! /n");
return;
}
FileCopy(fpin,fpout);
fclose(fpin);
fclose(fpout);
fpin = NULL;
fpout = NULL;
printf("~~~~~~~~~~~~~~~~~~");
return;
}
void FileCopy(FILE *fpin, FILE *fpout)
{
char ch;
ch = getc(fpin);
while(!feof(fpin))
{
putc(ch,fpout);
ch = getc(fpin);
}
}
求加分 哈哈
- 5楼网友:独钓一江月
- 2021-03-22 23:13
#include
int main()
{
FILE *fp1,*fp2;
char buf[1024];
fp1=fopen("C:/1.dat","rb");
fp2=fopen("C:/2.dat","wb");
fread(buf,1024,1,fp1);
while(!feof(fp1))
{
fwrite(buf,1024,1,fp2);
fread(buf,1024,1,fp1);
}
fclose(fp1);
fclose(fp2);
}
int main()
{
FILE *fp1,*fp2;
char buf[1024];
fp1=fopen("C:/1.dat","rb");
fp2=fopen("C:/2.dat","wb");
fread(buf,1024,1,fp1);
while(!feof(fp1))
{
fwrite(buf,1024,1,fp2);
fread(buf,1024,1,fp1);
}
fclose(fp1);
fclose(fp2);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯