c语言对文件操作的一点小问题
#include "stdafx.h"#include "stdio.h"
#include "stdlib.h"
int main()
{
FILE *fp;
char s[160],t;
int i,j,n,ni,ch;
if((fp=fopen("file1.txt","w"))==NULL)
{
printf("haha\n");
exit(0);
}
// else
//{
t=getchar();
while(t!='#')
{
fputc(t,fp);putchar(t);
t=getchar();
}
fclose(fp);
// }
//fclose(fp);
// if((fp=fopen("fi"")))
fp=fopen("file1.txt","r");
for(i=0;(ch=fgetc(fp)!=EOF);i++)
{
s[i]=ch;
}
ni=i;
if((fp=fopen("file2.txt","w"))==NULL)
{
printf("hehe\n");
exit(0);
}
else
{
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
}
}
fclose(fp);
fp=fopen("file2.txt","r");
for(i=ni;(ch=fgetc(fp)!=EOF);i++)
{
s[i]=ch;
}
n=i;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(s[i]>s[j])
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("\nfile3 文件内容是:\n");
fp=fopen("file3.txt","w");
for(i=0;i<n;i++)
{
fputc((s[i]),fp);
putchar(s[i]);
}
fclose(fp);
return 0;
}
本意是将file1.txt file2.txt里面的字母从小到大排序输出到file3.txt中,但file3里面出现乱码