#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define FILENAME "readfile.txt" //自己在这里写文件路径
#define NEWFILE "new.txt" //自己在这里写文件路径
int Procstr(char *str, int len)
{
int i, num = 0;
if (*str == '0')
{
for (i = 1; i < len; i ++)
{
num += (str[i]-'0')*pow(8, len-1-i);
}
}else
{
for (i = 0; i < len; i ++)
{
num += (str[i]-'0')*pow(10, len-1-i);
}
}
return num;
}
void Qsort(int *ary, int n)
{
int i, j;
for (i = 0; i < n; i ++)
{
for (j = i+1; j < n; j ++)
{
if (ary[i] < ary[j])
{
ary[i] = ary[i] + ary[j];
ary[j] = ary[i] - ary[j];
ary[i] = ary[i] - ary[j];
}
}
}
}
int *ReadFile(int *n)
{
FILE *fp;
char buf[16], c;
int len = 0, cnt = 0;
int *ary = NULL;
if ((fp = fopen(FILENAME, "r")) == NULL)
{
printf("fopen %s error !\n", FILENAME);
exit(1);
}
while (1)
{
c = fgetc(fp);
if (c == EOF)
{
if (len != 0)
{
ary = (int *)realloc(ary, sizeof(int)*(++cnt));
ary[cnt-1] = Procstr(buf, len);
len = 0;
printf("%d ", ary[cnt-1]);
}
break;
}
else if (c >= '0' && c <= '9')
{
if (len == 15)
{
printf("data len override!\n");
if (ary) free(ary);
fclose(fp);
exit(1);
}
buf[len ++] = c;
}else if (c == ',')
{
ary = (int *)realloc(ary, sizeof(int)*(++cnt));
ary[cnt-1] = Procstr(buf, len);
len = 0;
printf("%d ", ary[cnt-1]);
}
}
printf("\n");
fclose(fp);
*n = cnt;
return ary;
}
void WriteFile(int *a, int n)
{
FILE *fp;
int i;
if ((fp = fopen(NEWFILE, "w")) == NULL)
{
printf("fopen %s error !\n", NEWFILE);
free(a);
exit(1);
}
for (i = 0; i < n; i ++)
{
fprintf(fp, "%d ", a[i]);
}
fclose(fp);
}
int main(void)
{
int *ary = NULL, cnt=0;
ary = ReadFile(&cnt);
Qsort(ary, cnt);
WriteFile(ary, cnt);
free(ary);
}