#define _CRTDBG_MAP_ALLOC
#include
#include
#include
#include
#include
#define LEN sizeof(struct GeoFeature)
struct GeoFeature
{
long code;
int geotype;
int pointCount;
float* pX; //0点1线2面
float* pY;
struct GeoFeature* pNext;
};
void main()
{
FILE* fp ;
struct GeoFeature* pHead = NULL;
struct GeoFeature* pPrev = NULL;
struct GeoFeature* pCur = NULL;
struct GeoFeature* pNext = NULL;
int tempCode = 0;
int i = 0;
int j = 0;
long code;
long fpos = 0;
int pointCount;
int featureCount=0;
if ((fp = fopen("CHINA.ZBI", "r")) == NULL)
{
printf("The file 'CHINA.ZBI'was not opened\n");
return;
}
else
printf("The file 'CHINA.ZBI'was opened\n");
while (!feof(fp--))
{
fscanf(fp, "%ld %ld", &code, &tempCode);
if (tempCode == 0)
fscanf(fp, "%ld %ld", &pointCount, &tempCode);
if (featureCount == 0)
{
pHead = (struct GeoFeature*) malloc(LEN);
pCur = pHead;
memset(pCur, 0, LEN);
}
else
{
pCur->pNext = (struct GeoFeature*)malloc(LEN);
pCur = pCur->pNext;
memset(pCur, 0, LEN);
}
pCur->code = code;
pCur->pointCount = pointCount;
//如果点的个数为一。则几何类型为点。
if (pointCount == 1)
pCur->geotype = 0;
pCur->pX = (float*)calloc(pointCount, sizeof(float));
pCur->pY = (float*)calloc(pointCount, sizeof(float));
for (i = 0; i < pointCount; i++)
{
fscanf(fp, "%f %f", &(pCur->pX[i]), &(pCur->pY[i]));
}
if (pCur->pX[0] == pCur->pX[pointCount - 1] && pCur->pY[0] == pCur->pY[pointCount - 1])
pCur->geotype = 2;
else
pCur->geotype = 1;
featureCount++;
}
fclose(fp);
//打印输出
pCur = pHead;
while (pCur->pNext != NULL)
{
printf("%d,%d.%d\n", pCur->code,pCur->geotype, pCur->pointCount);
for (i = 0; i < pCur->pointCount; i++)
printf("%f,%f\n", pCur->pX[i], pCur->pY[i]);
pCur = pCur->pNext;
}
//删除内存
pCur = pHead;
while (pCur != NULL)
{
pPrev = pCur;
pCur = pCur->pNext;
free(pPrev->pX);
free(pPrev->pY);
free(pPrev);
}
_CrtDumpMemoryLeaks();
}