1.资源泄露 error: Resource leak: fpRead
增加fclose(fpRead);
int ReadSysParameters(struct DevGprsFlow* ptrSaveFlow, unsigned short* ptrCountSum, char* ptrFileName, char* ptrBakFileName)
{
int iIndex, jIndex;
int iReturn;
char ucString[256];
unsigned char ucBuffer[256];
unsigned char ucEndFlag[4];
unsigned short u2RecordSum = 0;
FILE* fpRead;
jIndex = 0;
fpRead = fopen(ptrFileName, "rb+");
if (fpRead == NULL)
{
PR("saveSysParameters: failed to open the config file.\n");
return(-1);
}
if (fpRead == NULL)
{
sprintf(ucString, "cp %s %s", ptrBakFileName, ptrFileName);
iReturn = _system(ucString);
if (iReturn == -1)
{
PR("file error1\n");
return(-1);
}
else
{
fpRead = fopen(ptrFileName, "rb+");
if (fpRead == NULL)
{
PR("file error2\n");
return(-1);
}
}
}
else
{
/*checking the files*/
fseek(fpRead, (0 - sizeof(ucEndFlag)), SEEK_END);
fread(&ucEndFlag[0], sizeof(ucEndFlag), 1, fpRead);
if (ucEndFlag[0] == 0xFF && ucEndFlag[1] == 0xFD && ucEndFlag[2] == 0xFF && ucEndFlag[3] == 0xFD)
{
fseek(fpRead, 0, SEEK_SET);
}
else
{
sprintf(ucString, "cp %s %s", ptrBakFileName, ptrFileName);
_system(ucString);
fclose(fpRead);//新增加,不然报错
//这里因为之前的文件没有关闭,所以要在新的fopen之前关闭不然resource leak
fpRead = fopen(ptrFileName, "rb+");
if (fpRead == NULL)
{
PR("file error3\n");
return(-1);
}
}
}
// while(!feof(fpRead))
// {
fread(ucBuffer, 1, 2, fpRead);
u2RecordSum = ucBuffer[0];
u2RecordSum <<= 8;
u2RecordSum += ucBuffer[1];
if (u2RecordSum > DEV_FLOW_CONUNTSUM)
u2RecordSum = DEV_FLOW_CONUNTSUM;
*ptrCountSum = u2RecordSum;
PR("u2RecordSum=%d\n", u2RecordSum);
for (iIndex = 0; iIndex < u2RecordSum; iIndex++)
{
fread(ptrSaveFlow, sizeof(struct DevGprsFlow), 1, fpRead);
PR("year=%04d%02d%02d,flow=%d\n", ptrSaveFlow->u2Year, ptrSaveFlow->u1Month, ptrSaveFlow->u1Day, ptrSaveFlow->u4GprsFlow);
ptrSaveFlow++;
}
// }
fclose(fpRead);
/*save the backup file*/
sprintf(ucString, "cp %s %s", ptrFileName, ptrBakFileName);
_system(ucString);
return(1);
}