うまくいけば、あなたは次のプログラムを探しています。これにより grades.txt が読み取られ、メモリが作成され、最終的に解放されます。次のプログラムをテストしましたが、正常に動作します。
#include "stdio.h"
int main(int argc, char *argv[])
{
FILE *fp;
int temp;
int *grades = NULL;
int count = 1;
int index;
fp = fopen("grades.txt","rb+");
while( fscanf(fp,"%d",&temp) != EOF )
{
if( grades == NULL )
{
grades = malloc(sizeof(temp));
*grades = temp;
printf("The grade is %d\r\n",temp);
}
else
{
printf("The grade is realloc %d\r\n",temp);
count++;
grades = realloc(grades,sizeof(grades)*count);
index = count -1;
*(grades+index) = temp;
//printf("the index is %d\r\n",index);
}
}
/** lets print the data now **/
temp = 0;
while( index >= 0 )
{
printf("the read value is %d\r\n",*(grades+temp));
index--;
temp ++;
}
fclose(fp);
free(grades);
grades = NULL;
}