#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 100
int main()
{
FILE *in,*out1,*out2;
char ch1,ch2;
char s[M],t;
int i=0,m,n,len;
//打開這三個文件
if((in=fopen("xiaosun.txt","r"))==NULL)
{
printf("error");
exit(0);
}
if((out1=fopen("text.txt","r"))==NULL)
{
printf("error");
exit(0);
}
if((out2=fopen("C.txt","w"))==NULL)
{
printf("error");
exit(0);
}
//將文件text中的內容,追加到xiaosun中的後面。
/* while((ch1=fgetc(in))!=EOF)//送入xiaosun文件中追加
{
fputc(ch1,out2);
// putchar(ch1);//把in指針的移動末尾
}
while((ch2=fgetc(out1))!=EOF)//送入xiaosun文件中追加
{
fputc(ch2,out2);
}*/
//把xiaosun文件的內容,複製到c中。
//對c文件中的字符進行排序
//out2的指針要重新回到開頭
while((ch1=fgetc(in))!=EOF)//送入xiaosun文件中追加
{
s[i++]=ch1;//把字符全部取出來放在數組s中進行排序操作
}
while((ch2=fgetc(out1))!=EOF)//送入xiaosun文件中追加
{
s[i++]=ch2;//把字符全部取出來放在數組s中進行排序操作
}
//進行冒泡排序
s[i]='\0';//記住這個地方一定要加,否則調試會有錯誤
len=strlen(s);
printf("字符串的長度爲:%d\n",len);
for(m=0;m<len-1;m++)
{
for(n=0;n<len-m-1;n++)
{
if(s[n]>s[n+1])
{
t=s[n];
s[n]=s[n+1];
s[n+1]=t;
}
}
}
fputs(s,out2);
fclose(in);
fclose(out1);
fclose(out2);
return 0;
}
非常需要注意是:字符串結束標誌一定要加上。
基本思想是:將xiaosun.txt中的內容和text.txt中的內容取出來放在字符數組s中,對數組中的字符進行排序,然後把字符串在寫入c文件中。
譚浩強第四版文件那章的系統第四題。