C语言~将两个文本文件的内容合并后存入另一文件中,试编写相应程序.
编程如下,请问为什么我打开3.txt中却多了两个字符(在txt中显示如图),复制过来就是个空格。。。。哪位知道是什么原因,有什么办法去掉呢#include<stdio.h...
编程如下,请问为什么我打开3.txt中却多了两个字符(在txt中显示如图),复制过来就是个空格。。。。哪位知道是什么原因,有什么办法去掉呢
#include <stdio.h>
#include <process.h>
void main()
{FILE *fp1,*fp2,*fp3;
if((fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\1.txt","r"))==NULL)
{printf("打开文件1.txt失败!\n");
exit(1);
}
else
printf("打开文件1.txt成功!\n");
if((fp2=fopen("C:\\Documents and Settings\\Administrator\\桌面\\2.txt","r"))==NULL)
{printf("打开文件2.txt失败!\n");
exit(3);
}
else
printf("打开文件2.txt成功!\n");
if((fp3=fopen("C:\\Documents and Settings\\Administrator\\桌面\\3.txt","a"))==NULL)
{printf("打开文件3.txt失败!\n");
exit(3);
}
else
printf("打开文件3.txt成功!\n");
while(!feof(fp1))
fputc(fgetc(fp1),fp3);
while(!feof(fp2))
fputc(fgetc(fp2),fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
OK!!按一楼的该法问题解决,不过我百度百科feof发现下面那样修改(继续用feof)也可以。。。原因也写得很清楚,有兴趣就百科看看,同样感谢2楼~~
ch=fgetc(fp1);
while(!feof(fp1))
{fputc(ch,fp3);
ch=fgetc(fp1);}
ch=fgetc(fp2);
while((ch=fgetc(fp2))!=EOF)
{fputc(ch,fp3);
ch=fgetc(fp2);} 展开
#include <stdio.h>
#include <process.h>
void main()
{FILE *fp1,*fp2,*fp3;
if((fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\1.txt","r"))==NULL)
{printf("打开文件1.txt失败!\n");
exit(1);
}
else
printf("打开文件1.txt成功!\n");
if((fp2=fopen("C:\\Documents and Settings\\Administrator\\桌面\\2.txt","r"))==NULL)
{printf("打开文件2.txt失败!\n");
exit(3);
}
else
printf("打开文件2.txt成功!\n");
if((fp3=fopen("C:\\Documents and Settings\\Administrator\\桌面\\3.txt","a"))==NULL)
{printf("打开文件3.txt失败!\n");
exit(3);
}
else
printf("打开文件3.txt成功!\n");
while(!feof(fp1))
fputc(fgetc(fp1),fp3);
while(!feof(fp2))
fputc(fgetc(fp2),fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
OK!!按一楼的该法问题解决,不过我百度百科feof发现下面那样修改(继续用feof)也可以。。。原因也写得很清楚,有兴趣就百科看看,同样感谢2楼~~
ch=fgetc(fp1);
while(!feof(fp1))
{fputc(ch,fp3);
ch=fgetc(fp1);}
ch=fgetc(fp2);
while((ch=fgetc(fp2))!=EOF)
{fputc(ch,fp3);
ch=fgetc(fp2);} 展开
展开全部
以下程序调试成功,我一直使用(ch=fgetc(fp1))!=EOF来判断文件尾,没有出错过,至于为什么的问题我就不知道了
#include <stdio.h>
#include <process.h>
void main()
{
char ch;
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\1.txt","r"))==NULL)
{printf("打开文件1.txt失败!\n");
exit(1);
}
else
printf("打开文件1.txt成功!\n");
if((fp2=fopen("C:\\Documents and Settings\\Administrator\\桌面\\2.txt","r"))==NULL)
{printf("打开文件2.txt失败!\n");
exit(3);
}
else
printf("打开文件2.txt成功!\n");
if((fp3=fopen("C:\\Documents and Settings\\Administrator\\桌面\\3.txt","a"))==NULL)
{printf("打开文件3.txt失败!\n");
exit(3);
}
else
printf("打开文件3.txt成功!\n");
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
#include <stdio.h>
#include <process.h>
void main()
{
char ch;
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("C:\\Documents and Settings\\Administrator\\桌面\\1.txt","r"))==NULL)
{printf("打开文件1.txt失败!\n");
exit(1);
}
else
printf("打开文件1.txt成功!\n");
if((fp2=fopen("C:\\Documents and Settings\\Administrator\\桌面\\2.txt","r"))==NULL)
{printf("打开文件2.txt失败!\n");
exit(3);
}
else
printf("打开文件2.txt成功!\n");
if((fp3=fopen("C:\\Documents and Settings\\Administrator\\桌面\\3.txt","a"))==NULL)
{printf("打开文件3.txt失败!\n");
exit(3);
}
else
printf("打开文件3.txt成功!\n");
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询