C语言文件处理问题!找不到错误原因,求大神帮忙!
void paixu(int *p, int n){
int i, j;
for (i = 0; i < n; i++)
{
for (j = 1; j < n-i; j++)
{
if (p[j-1] > p[j])
{
int tmp = p[j-1];
p[j-1] = p[j];
p[j] = tmp;
}
}
}
}
int main()
{
int len[1];
int cons = 0;
// int max[100];
FILE *ps = fopen("f:\\a.txt", "r");
while (!feof(ps))
{
memset(len, 0, sizeof(len));
fgets(len, sizeof(int), ps);
cons++;
}
fclose(ps);
int *max = calloc(cons, sizeof(int));
cons = 0;
FILE *p = fopen("f:\\a.txt", "r");
while (!feof(p))
{
memset(len, 0, sizeof(len));
fgets(len, sizeof(int), p);
max[cons] = atoi(len);
cons++;
}
fclose(p);
FILE *pi = fopen("f:\\b.txt", "w");
paixu(&max, cons);
int i;
for (i = 0; i < cons; i++)
{
memset(len, 0, sizeof(len));
sprintf(len,"%d\n", max[i]);
fputs(len, pi);
}
fclose(pi);
free(max);
return 0;
}
//int max[100];形式 程序无错误! 但是在堆里程序就错误!找不到错误原因求大神帮忙看看!