第2个回答 2014-12-30
#include<stdio.h>
#include<string.h>
void sort(char str[][256],int n)
{ int i,j;
char buffer[256];
FILE *fp;
for ( i=0;i<n-1;i++ )
for ( j=i+1;j<n;j++ )
if ( strcmp(str[i],str[j] )>0 )
{ strcpy(buffer,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],buffer);
}
if ( fp=fopen("data.txt","w+") )
{ for ( i=0;i<n;i++ )
{ sprintf(buffer,"%s\n",str[i]);
fputs(buffer);
}
fclose(fp);
} else printf("无法建立文件。\n");
}
void inOut(char str[][256],int n)
{ FILE *fp;
int m,i;
m=i=0;
if ( fp=fopen("data.txt","r") )
{ while ( !feof(fp) )
{ fscanf(fp,"%s",str[m]);
if ( i%2==0 ) { printf("%s\n",str[m]); m++; if ( m>=n ) break; }
i++;
}
fclose(fp);
} else printf("data.txt无法打开读取数据。\n");
}
void main()
{ char str[8][256];
int i;
for ( i=0;i<8;i++ ) scanf("%s",str[i]);
sort(str,8);
inOut();
}本回答被网友采纳