c语言:有两个磁盘文件 A.txt 和 B.txt,各存放一行字母,要求把这两个文件中的信息合并到一个新文件 C 中
#include<stdio.h>intmain(){FILE*f1,*f2,*f3;charch;if(!(f1=fopen("d:\\A.txt","w"))){pr...
#include <stdio.h>
int main ()
{
FILE *f1,*f2,*f3;
char ch;
if(!(f1=fopen("d:\\A.txt","w")))
{
printf("error");
return 0;
}
if(!(f2=fopen("d:\\B.txt","w")))
{
printf("error");
return 0;
}
if(!(f3=fopen("d:\\C.txt","w")))
{
printf("error");
return 0;
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f1);
ch=getchar();
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f2);
ch=getchar();
}
f1=fopen("d:\\A.txt","r");
f2=fopen("d:\\B.txt","r");
while((ch=fgetc(f1))!=EOF)
fputc(ch,f3);
if(!(f3=fopen("C.txt","a")))
{
printf("error");
return 0;
}
while((ch=fgetc(f2))!=EOF)
fputc(ch,f3);
fclose(f1);
fclose(f2);
fclose(f3);
}
这是本人的程序 为什么c文件中总是没东西 求各位大神指导 展开
int main ()
{
FILE *f1,*f2,*f3;
char ch;
if(!(f1=fopen("d:\\A.txt","w")))
{
printf("error");
return 0;
}
if(!(f2=fopen("d:\\B.txt","w")))
{
printf("error");
return 0;
}
if(!(f3=fopen("d:\\C.txt","w")))
{
printf("error");
return 0;
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f1);
ch=getchar();
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f2);
ch=getchar();
}
f1=fopen("d:\\A.txt","r");
f2=fopen("d:\\B.txt","r");
while((ch=fgetc(f1))!=EOF)
fputc(ch,f3);
if(!(f3=fopen("C.txt","a")))
{
printf("error");
return 0;
}
while((ch=fgetc(f2))!=EOF)
fputc(ch,f3);
fclose(f1);
fclose(f2);
fclose(f3);
}
这是本人的程序 为什么c文件中总是没东西 求各位大神指导 展开
1个回答
展开全部
1、文件没有关闭而打开了两次
2、打开c文件在写c文件之后
#include <stdio.h>
int main ()
{
FILE *f1,*f2,*f3;
char ch;
if(!(f1=fopen("d:\\A.txt","w")))
{
printf("error");
return 0;
}
if(!(f2=fopen("d:\\B.txt","w")))
{
printf("error");
return 0;
}
if(!(f3=fopen("d:\\C.txt","w")))
{
printf("error");
return 0;
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f1);
ch=getchar();
}
ch=getchar();
while(ch!=EOF)
{
fputc(ch,f2);
ch=getchar();
}
fclose(f1);
fclose(f2);
fclose(f3);
f1=fopen("d:\\A.txt","r");
f2=fopen("d:\\B.txt","r");
if(!(f3=fopen("d:\\C.txt","a")))
{
printf("error");
return 0;
}
while((ch=fgetc(f1))!=EOF)
fputc(ch,f3);
while((ch=fgetc(f2))!=EOF)
fputc(ch,f3);
fclose(f1);
fclose(f2);
fclose(f3);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |