文件操作+排序...
代码
#include
<
stdio.h
>
#include < stdlib.h >
#define F_NAME_LEN 20
#define MAX_LEN 10000
/* 函数说明 */
void get_data( int * , int * ); /* 返回此数组的长度 */
int sort_function( const void * , const void * ); /* 排序的规则 */
void release_data( int * , int ); /* 将排序后的数据输出到文件中 */
int main()
{
int src[MAX_LEN], len; /* src用于保存整数 */
get_data(src, & len);
qsort(src, len, sizeof ( int ), sort_function);
release_data(src, len);
}
void release_data( int * src, int len)
{
FILE * fp = NULL; /* 输出的目标文件 */
int * begin, * end;
if ((fp = fopen( " dest.txt " , " w " )) == NULL)
{
printf( " file open error!\npress any key to exit...\n " );
exit( - 1 );
}
begin = src;
end = src + len;
while (begin != end)
{
fprintf(fp, " %d " , * begin ++ );
fprintf(fp, " %c " , ' ' );
}
fclose(fp);
}
/* 从文件中读取数据 */
void get_data( int * src, int * len)
{
FILE * fp = NULL;
char fname[F_NAME_LEN], c;
int num;
printf( " Enter file name:\n " );
scanf( " %s " , fname);
getchar();
if ((fp = fopen(fname, " r " )) == NULL)
{
printf( " file open error!\npress any key to exit...\n " );
getchar();
exit( - 1 );
}
num = * len = 0 ;
while ((c = fgetc(fp)) != EOF) /* 不断读取 */
{
if (c >= 48 && c <= 57 ) /* 是数字 */
{
num = num * 10 + c - 48 ;
}
else /* 非数字 */
{
src[( * len) ++ ] = num;
num = 0 ;
while ((c = fgetc(fp)) != EOF) /* 去除数字的其余符号 */
{
if ( ! (c >= 48 && c <= 57 ))
{
continue ;
}
else
{
num = num * 10 + c - 48 ; /* 已经读取了一位 */
break ;
}
}
}
}
if (num > 0 ) /* 末尾可能有一位 */
{
src[( * len) ++ ] = num;
}
fclose(fp);
}
/* 排序规则 */
int sort_function( const void * a, const void * b)
{
return * (( int * )a) - * (( int * )b);
}
#include < stdlib.h >
#define F_NAME_LEN 20
#define MAX_LEN 10000
/* 函数说明 */
void get_data( int * , int * ); /* 返回此数组的长度 */
int sort_function( const void * , const void * ); /* 排序的规则 */
void release_data( int * , int ); /* 将排序后的数据输出到文件中 */
int main()
{
int src[MAX_LEN], len; /* src用于保存整数 */
get_data(src, & len);
qsort(src, len, sizeof ( int ), sort_function);
release_data(src, len);
}
void release_data( int * src, int len)
{
FILE * fp = NULL; /* 输出的目标文件 */
int * begin, * end;
if ((fp = fopen( " dest.txt " , " w " )) == NULL)
{
printf( " file open error!\npress any key to exit...\n " );
exit( - 1 );
}
begin = src;
end = src + len;
while (begin != end)
{
fprintf(fp, " %d " , * begin ++ );
fprintf(fp, " %c " , ' ' );
}
fclose(fp);
}
/* 从文件中读取数据 */
void get_data( int * src, int * len)
{
FILE * fp = NULL;
char fname[F_NAME_LEN], c;
int num;
printf( " Enter file name:\n " );
scanf( " %s " , fname);
getchar();
if ((fp = fopen(fname, " r " )) == NULL)
{
printf( " file open error!\npress any key to exit...\n " );
getchar();
exit( - 1 );
}
num = * len = 0 ;
while ((c = fgetc(fp)) != EOF) /* 不断读取 */
{
if (c >= 48 && c <= 57 ) /* 是数字 */
{
num = num * 10 + c - 48 ;
}
else /* 非数字 */
{
src[( * len) ++ ] = num;
num = 0 ;
while ((c = fgetc(fp)) != EOF) /* 去除数字的其余符号 */
{
if ( ! (c >= 48 && c <= 57 ))
{
continue ;
}
else
{
num = num * 10 + c - 48 ; /* 已经读取了一位 */
break ;
}
}
}
}
if (num > 0 ) /* 末尾可能有一位 */
{
src[( * len) ++ ] = num;
}
fclose(fp);
}
/* 排序规则 */
int sort_function( const void * a, const void * b)
{
return * (( int * )a) - * (( int * )b);
}