首先,定義直線、圓的結構體,把它們儲存在ineStruct.h中
//#include "lineStruct.h"
#define STRLEN 60
#define DATASIZE sizeof(EntityData)
/*-----每個實體的結構-----*/
//你可在在此添加其它的實體
//為了提高精度,變量可定義為雙精度型
typedef struct tagLine{
float x1,y1,z1;
float x2,y2,z2;
}LINE;
typedef struct tagCircle{
float x,y,z;
float radius;
}CIRCLE;
/*------------------------*/
typedef union specialData{
LINE line;
CIRCLE circle;
}privateData;
/*------實體的資料結構-------*/
typedef struct commonData{
char id[STRLEN]; /*實體辨別字元串*/
char layer[STRLEN]; /*層名字元串*/
privateData data; /*特有資料塊*/
struct commonData *next; /*用于建構連結清單*/
}EntityData;
//定義完資料結構後,就可以用連結清單結構來存儲實體中有用的資訊了。
//以下程式為讀取實體LINE的有關資訊的代碼。
/*------------------------------------------
*Entity.C 讀取實體LINE部分内容。
*-------------------------------------------*/
然後,在line.cpp中實作讀取直線的數量、直線兩端端點的坐标
//line.cpp
#include "lineStruct.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
/*----------函數聲明部分-----------*/
void print(EntityData *entity);
/*---------------------------------*/
int main(int argc,char *argv[])
{
int code;
float value;
char codevalue[STRLEN];
FILE *dxf;
char filename[STRLEN];
char suffix[6] = ".dxf";
EntityData *entity,*entity1,*entity2;
printf("請輸入DXF檔案名:");
gets(filename);
strcat(filename,suffix);
dxf = fopen(filename,"r");
if(!dxf) {
printf("打開檔案出錯!\n可能不存在此檔案.\n");
printf("按任意鍵退出...");
getch();
exit(0);
}
else {
printf("正在讀取檔案...\n");
}
entity = entity2 = (EntityData *)malloc(DATASIZE);
while(!feof(dxf)) {
fscanf(dxf,"%d",&code);
fscanf(dxf,"%s",codevalue);
if(code == 2 && strcmp(codevalue,"ENTITIES")==0) {
while(strcmp(codevalue,"ENDSEC")) {
fscanf(dxf,"%d",&code);
fscanf(dxf,"%s",codevalue);
if(code == 0 && strcmp(codevalue,"LINE")==0) {
entity1 = (EntityData *)malloc(DATASIZE);
strcpy(entity1->id,codevalue);
fscanf(dxf,"%d",&code);
while(code) {
switch(code) {
case 8:
fscanf(dxf,"%s",codevalue);
fscanf(dxf,"%d",&code);
strcpy(entity1->layer,codevalue);
break;
case 10:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.x1 = value;
break;
case 20:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.y1 = value;
break;
case 30:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.z1 = value;
break;
case 11:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.x2 = value;
break;
case 21:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.y2 = value;
break;
case 31:
fscanf(dxf,"%f",&value);
fscanf(dxf,"%d",&code);
entity1->data.line.z2 = value;
break;
default: {
fscanf(dxf,"%s",codevalue);
fscanf(dxf,"%d",&code);
}
}
}
entity2->next = entity1;
entity2 = entity1;
}
}
entity2->next = NULL;
}
}
entity = entity->next; //第一個實體區為空,是以使頭指針移向下一個實體
print(entity); //輸對外連結表
printf("\nPress any key to halt...");
getch();
return 0;
}
//輸對外連結表
void print(EntityData *entity)
{
int i=0;
EntityData *pointer;
pointer = entity;
if(pointer != NULL) {
do{
i++;
pointer = pointer->next;
}while(pointer != NULL);
}
printf("\nOutput LinkList:");
printf("\nDXF檔案中總共有%d條直線:\n",i);
i = 1;
pointer = entity;
if(pointer != NULL) {
do{
printf("第%d條直線:\n",i);
printf("X1=%f\tY1=%f\tZ1=%f\n",pointer->data.line.x1,
pointer->data.line.y1,pointer->data.line.z1);
printf("X2=%f\tY2=%f\tZ2=%f\n",pointer->data.line.x2,
pointer->data.line.y2,pointer->data.line.z2);
pointer = pointer->next;
i++;
}while(pointer !=NULL);
}
}
效果如下:
參考位址:
http://blog.csdn.net/eryar/article/details/2371755