主题:文件合并入数组出来乱码
#include "stdio.h"
#include "stdlib.h"
main()
{ FILE *fp,*ff,*fw;/*定义三个文件指针*/
char c[14];/*定义一个数组用来盛放文件的内容*/
char ch;
int i=0,x;
if((fp=fopen("a.txt","w"))==NULL)/*fp与ff都已w的形式打开然后输入到其中若干字符 到 遇到’a'为止,结束输入*/
{ printf("Can not open:"); exit(0);}
printf("\n kai shi shu ru A.txt :\n");
ch=getchar();
while(ch!='a')
{
fputc(ch,fp);/*在fp中输入字符*/
ch=getchar();
}
fclose(fp);
printf("\n kai shi shu ru B.txt:\n");
if((ff=fopen("b.txt","w"))==NULL)
{ printf("Can not open:"); exit(0);}
ch=getchar();
while(ch!='a')/*在ff所指向的文件中输入字符*/
{
fputc(ch,ff);
ch=getchar();
}
fclose(ff);
printf("\n kai shi he bing :\n");
if((fp=fopen("a.txt","r"))==NULL)
{ printf("Can not open:"); exit(0);}
if((ff=fopen("b.txt","a"))==NULL)/*用a的形式打开b.txt将a.txt中的字符,加入到b.txt中*/
{ printf("Can not open:"); exit(0);}
ch=fgetc(fp);
while(ch!=EOF)
{ fputc(ch,ff);/*用a的形式打开b.txt将a.txt中的字符,加入到b.txt中*/
ch=fgetc(fp);
}
fclose(ff);
fclose(fp);
if((ff=fopen("b.txt","r"))==NULL)/*用r的形式打开b.txt将文件读出,依次写入到c[i]中*/
{ printf("Can not open:"); exit(0);}
ch=fgetc(ff);
while(ch!=EOF)
{ c[i]=ch;/*用r的形式打开b.txt将文件读出,依次写入到c[i]中*/
ch=fgetc(ff);
i++;
}
x=strlen(c);/*用x获得数组c的大小*/
fclose(ff);
printf("\nkai shi shu chu:\n");
for(i=0;i<x;i++)
printf("%c",c[i]);/*输出这个数组*/
getch();
}
问题就在于,我编译的时候,没错,运行的时候,数组是输出了文件b。txt的内容,但是,在输出的结果的后面,就是好几个乱码了。恩,这个到底是什么事啊?难道是数组定义的过大?还是x不能准确的获得数组的大小?请高手帮忙[em18][em18]
#include "stdlib.h"
main()
{ FILE *fp,*ff,*fw;/*定义三个文件指针*/
char c[14];/*定义一个数组用来盛放文件的内容*/
char ch;
int i=0,x;
if((fp=fopen("a.txt","w"))==NULL)/*fp与ff都已w的形式打开然后输入到其中若干字符 到 遇到’a'为止,结束输入*/
{ printf("Can not open:"); exit(0);}
printf("\n kai shi shu ru A.txt :\n");
ch=getchar();
while(ch!='a')
{
fputc(ch,fp);/*在fp中输入字符*/
ch=getchar();
}
fclose(fp);
printf("\n kai shi shu ru B.txt:\n");
if((ff=fopen("b.txt","w"))==NULL)
{ printf("Can not open:"); exit(0);}
ch=getchar();
while(ch!='a')/*在ff所指向的文件中输入字符*/
{
fputc(ch,ff);
ch=getchar();
}
fclose(ff);
printf("\n kai shi he bing :\n");
if((fp=fopen("a.txt","r"))==NULL)
{ printf("Can not open:"); exit(0);}
if((ff=fopen("b.txt","a"))==NULL)/*用a的形式打开b.txt将a.txt中的字符,加入到b.txt中*/
{ printf("Can not open:"); exit(0);}
ch=fgetc(fp);
while(ch!=EOF)
{ fputc(ch,ff);/*用a的形式打开b.txt将a.txt中的字符,加入到b.txt中*/
ch=fgetc(fp);
}
fclose(ff);
fclose(fp);
if((ff=fopen("b.txt","r"))==NULL)/*用r的形式打开b.txt将文件读出,依次写入到c[i]中*/
{ printf("Can not open:"); exit(0);}
ch=fgetc(ff);
while(ch!=EOF)
{ c[i]=ch;/*用r的形式打开b.txt将文件读出,依次写入到c[i]中*/
ch=fgetc(ff);
i++;
}
x=strlen(c);/*用x获得数组c的大小*/
fclose(ff);
printf("\nkai shi shu chu:\n");
for(i=0;i<x;i++)
printf("%c",c[i]);/*输出这个数组*/
getch();
}
问题就在于,我编译的时候,没错,运行的时候,数组是输出了文件b。txt的内容,但是,在输出的结果的后面,就是好几个乱码了。恩,这个到底是什么事啊?难道是数组定义的过大?还是x不能准确的获得数组的大小?请高手帮忙[em18][em18]