调试后,我收到此错误消息:正常块后检测到 HEAP CORRUPTION (#55)。如果我增加数组的大小(no_of_days+1)
,它可以工作,但我不知道它是否正确。我做错了什么?
int main()
{
int no_of_days=0;
int no_operations=0;
int *a;
int i;
FILE *pfile1=NULL;
FILE *pfile2=NULL;
char *filename1="input.txt";
char *filename2="output.txt";
pfile1=fopen(filename1, "r");
if(pfile1==NULL)
{
printf("Error on opening the file %s.",filename1);
return 0;
}
fscanf(pfile1,"%d%d",&no_of_days,&no_operations);
a=(int*)malloc((no_of_days)*sizeof(int));
if(a==NULL)
{
printf("Memory allocation failed.");
return 0;
}
for(i=1;i<=no_of_days;i++)
{
fscanf(pfile1,"%d",&a[i]);
}
pfile2=fopen(filename2,"a");
if(pfile2==NULL)
{
printf("Memory allocation failed!",filename2);
}
for(i=1;i<=no_of_days;i++)
{
fprintf(pfile2,"%d",a[i]);
}
free(a);
fclose(pfile1);
fclose(pfile2);