"void *" 类型的值不能用于初始化 "int *" 类型的实体
程序代码:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void pupple(int *p, int n)
{
int i;
int j;
for (i = 0; i < n; i++) {
for (j = 1; j < n - i; j++) {
if (p[j - 1]>p[j])
swap(&p[j - 1], &p[j]);
}
}
}
int main()//在堆中建立一个动态数组,并对文件内容进行排序
{
char buf[100];
int index = 0;
FILE *p = fopen("E:\\temp\\a.txt", "r");//第一次打开a.txt目的是知道文件有多少行
while (!feof(p)) {
memset(buf, 0, sizeof(buf));//每次读取文件一行前把这个buf清空
fgets(buf, sizeof(buf), p);//从文件中读一行
index++;//只计数v
}
fclose(p);
int *array = calloc(sizeof(int), index);//在堆中建立一个动态数组,动态数组的成员数量和a.txt文件的行一样多
FILE *p1 = fopen("E:\\temp\\a.txt", "r");
index = 0;//计数器从0重新开始
while (!feof(p1)) {
memset(buf, 0, sizeof(buf));//每次读取文件一行前把这个buf清空
fgets(buf, sizeof(buf), p1);//从文件中读一行
index++;//只计数v
}
fclose(p1);
pupple(array, index);//将数组排序
p1 = fopen("E:\\temp\\b.txt", "w");
int i;
for (i = 0; i < index; i++) {
memset(buf, 0, sizeof(buf));
sprintf(buf, "%d\n", array[i]);//数组成员转化为字符串
fputs(buf, p1);
}
fclose(p1);
getchar();
return 0;
}


