/*
* Description:
* 文件中有一个数组,排序后输出到另一文件 的c代码实现
* Author :FinL
* Language: C
* Date : 2010-08-29
*/
#include
#include
void bubble_sort(int *a,int n)
{
int tmp;
int i,j;
bool exchange=1;
for(i=0;i
{
exchange=0;
for(j=0;j
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
exchange=1;
}
}
}
}
int main()
{
int tmp;
int i = 0,j;
int num=0;
int* arry;
FILE *fp = fopen("int.txt","r");
if(NULL==fp)
{
printf("Open file int.txt failed.\n");
exit(0);
}
FILE *fp1 = fopen("int_sort.txt","w");
if(NULL==fp1)
{
printf("Open file int_sort.txt failed.\n");
exit(0);
}
while(fscanf(fp,"%d\n",&tmp)!=-1){
num++;
}
printf("\n");
fseek(fp,0,SEEK_SET);
printf("num : %d",num);
printf("\n");
arry = (int *)malloc(num);
while(fscanf(fp,"%d\n",&tmp)!=-1){
arry[i++]=tmp;
}
for(j=0;j
{
printf("%d ",arry[j]);
}
printf("\n");
fclose(fp);
// arry after sorting.
bubble_sort(arry,num);
for(j=0;j
printf("%d ",arry[j]);
}
printf("\n");
for(j=0;j
{
fprintf(fp1,"%d ",arry[j]);
}
fclose(fp1);
return 0;
}
阅读(1015) | 评论(0) | 转发(0) |