1 代码示例

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
void show(char aa[])
{
int i;
for(i=0;i<8;i++)
printf("%c",aa[i]);
printf("\n");
}
void sh_handle()
{
}
int OnChangeByteOrder(int indata)
{
char ss[9];
char ee[8];
unsigned long val=unsigned long(indata);
ultoa(val,ss,16);// 将十六进制的数(val)转到一个字符串(ss)中,itoa(val,ss,16);
int i;
int length=strlen(ss);
if(length!=8)
{
for(i=0;i<8-length;i++)
ee[i]='0';
for(i=0;i<length;i++)
ee[i+8-length]=ss[i];
for(i=0;i<8;i++)
ss[i]=ee[i];
}
////****** 进行倒序
int t;
t=ss[0];ss[0]=ss[6];ss[6]=t;
t=ss[1];ss[1]=ss[7];ss[7]=t;
t=ss[2];ss[2]=ss[4];ss[4]=t;
t=ss[3];ss[3]=ss[5];ss[5]=t;
////******
//****** 将存有十六进制数 (val) 的字符串 (ss) 中的十六进制数转成十进制数
int value=0;
for(i=0;i<8;i++)
{
int k;
if(ss[i]=='a'||ss[i]=='b'||ss[i]=='c'||ss[i]=='d'||ss[i]=='e'||ss[i]=='f')
k=10+ss[i]-'a';
else
k=ss[i]-'0';
//printf("k=%d\n",k);
value=value+int(k*pow(16,7-i));
}
//printf("value=%d\n",value);
return(value);
}
void OnWriteLineShp(FILE * ShpFile_fp)
{
}
void OnReadLineShp(FILE * ShpFile_fp)
{
// 读取线状目标的实体信息
FILE * fp;
if((fp=fopen("F:\\学习\\C++实验\\武汉shp\\桥梁_polyline.dxf","w"))==NULL)  //新建一个dxf文件  
{
exit(0);
}
//------------------------------------------------------
//写入dxf文件中,在新建的dxf中写入下列内容
fprintf(fp,"0\nSECTION\n");//fprintf( FILE *stream, const char *format, [ argument ]...),fprintf()函数根据指定的格式(format)向输出流(stream)写入数据(argument)
fprintf(fp,"2\nENTITIES\n");//fprintf( FILE *stream, const char *format, [ argument ]...),fprintf()函数根据指定的格式(format)向输出流(stream)写入数据(argument)
//------------------------------------------------------
int i,j;
int RecordNumber;
int ContentLength;
int num=0;
while((fread(&RecordNumber,sizeof(int),1,ShpFile_fp)!=0))//从ShpFile_fp中以int型字节的长度,读取一次,并将其读取到RecordNumber中
{   
//printf("转换前RecordNumber=%d\n",RecordNumber);
RecordNumber=OnChangeByteOrder(RecordNumber);//对RecordNumber进行转化后打印输出
printf("\n\nRecordNumber=%d\n",RecordNumber);
//fprintf(fp,"\n\nRecordNumber=%d\n",RecordNumber);
fread(&ContentLength,sizeof(int),1,ShpFile_fp);//从ShpFile_fp中以int型字节的长度,读取一次,并将其读取到ContentLength中
//printf("转换前ContentLength=%d\n",ContentLength);
ContentLength=OnChangeByteOrder(ContentLength);//对其进行转化 并输出
printf("ContentLength=%d\n",ContentLength);
//fprintf(fp,"ContentLength=%d\n",ContentLength);
int shapeType;
double Box[4];
int NumParts;
int NumPoints;
int *Parts;
fread(&shapeType,sizeof(int),1,ShpFile_fp);//从ShpFile_fp中以int型字节的长度,读取一次,并将其读取到ContentLength中
printf("shapeType=%d\n",shapeType);//打印输出
//fprintf(fp,"shapeType=%d\n",shapeType);
for(i=0;i<4;i++)
{
fread(Box+i,sizeof(double),1,ShpFile_fp); // 读 Box
printf("Box+%d=%lf\n",i,*(Box+i));
}
fread(&NumParts,sizeof(int),1,ShpFile_fp);// 读 NumParts 
printf("NumParts=%d\n",NumParts);
//fprintf(fp,"NumParts=%d\n",NumParts);
fread(&NumPoints,sizeof(int),1,ShpFile_fp);// 读NumPoints
printf("NumPoints=%d\n",NumPoints);
//fprintf(fp,"NumPoints=%d\n",NumPoints);
Parts=new int[NumParts];
for(i=0;i<NumParts;i++)
{
fread(Parts+i,sizeof(int),1,ShpFile_fp);
printf("Parts+%d=%d\n",i,*(Parts+i));
}
int pointNum;
for(i=0;i<NumParts;i++)
{
if(i!=NumParts-1)
pointNum = Parts[i+1]-Parts[i];
else
pointNum = NumPoints-Parts[i];
double *PointsX;
double *PointsY;
PointsX =new double[pointNum];
PointsY =new double[pointNum];
//读取坐标点
//------------------------------------------------------
fprintf(fp,"0\nPOLYLINE\n");在新建的dxf文件中写入信息
fprintf(fp,"8\n0\n");
fprintf(fp,"66\n1\n");
//------------------------------------------------------
for(j=0;j<pointNum;j++)
{
fread(PointsX+j, sizeof(double),1,ShpFile_fp);//读取点的X坐标
fread(PointsY+j, sizeof(double),1,ShpFile_fp);//读取点的Y坐标
printf("X[%d]=%lf\n",j,*(PointsX+j));//打印输出点的X坐标
printf("Y[%d]=%lf\n",j,*(PointsY+j));//打印输出点的Y坐标
//fprintf(fp,"X[%d]=%lf\n",j,*(PointsX+j));
//fprintf(fp,"Y[%d]=%lf\n",j,*(PointsY+j));
//------------------------------------------------------
fprintf(fp,"0\nVERTEX\n");//写入VERTEX
fprintf(fp,"8\n0\n");
fprintf(fp,"10\n%.9lf\n",*(PointsX+j));//写入点X坐标
fprintf(fp,"20\n%.9lf\n",*(PointsY+j));//写入点Y坐标
//------------------------------------------------------
}
//------------------------------------------------------
//写入dxf文件中
fprintf(fp,"0 \nSEQEND\n");
fprintf(fp,"8 \n0\n");
//------------------------------------------------------
delete[] PointsX;
delete[] PointsY;
}
delete[] Parts;
}
//------------------------------------------------------
fprintf(fp,"0 \nENDSEC\n");
fprintf(fp,"0 \nEOF");
fclose(fp);
//------------------------------------------------------
}
void main()
{
//----------------------------------------------------------------
// 打开坐标文件
FILE * m_ShpFile_fp;
if((m_ShpFile_fp=fopen("F:\\学习\\C++实验\\武汉shp\\桥梁_polyline.shp","rb"))==NULL)    
{
exit(0);
}
//----------------------------------------------------------------
// 读取坐标文件头的内容开始
int fileCode =-1;
int fileLength=-1;
int version=-1;
int shapeType=-1;
int i;
int FileCode;
int Unused;
int FileLength;
int Version;
int ShapeType;
double Xmin;
double Ymin;
double Xmax;
double Ymax;
double Zmin;
double Zmax;
double Mmin;
double Mmax;
fread(&FileCode,sizeof(int),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Filecode里面去,每次读一个int型字节的长度,读取一次
printf("FileCode=%d\n",FileCode);//在屏幕上打印FileCode的值
FileCode = OnChangeByteOrder(FileCode);//将读取的FileCode的值转化为十进制的数
printf("FileCode=%d\n",FileCode);//打印转化后的十进制的FileCode的值
for(i=0;i<5;i++)
fread(&Unused,sizeof(int),1,m_ShpFile_fp);
fread(&FileLength,sizeof(int),1,m_ShpFile_fp);//读取FileLength
FileLength=OnChangeByteOrder(FileLength);//将FileLength转化为十进制的数
printf("FileLength=%d\n",FileLength);//在屏幕上打印输出
fread(&Version,sizeof(int),1,m_ShpFile_fp);//读取Version的值
printf("Version=%d\n",Version);//在屏幕上打印输出
fread(&ShapeType,sizeof(int),1,m_ShpFile_fp);//读取ShapeType的值
printf("ShapeType=%d\n",ShapeType);//在屏幕上打印输出ShapeType的值
fread(&Xmin,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Xmin里面去,每次读取一个double型字节长度,读取一次
printf("Xmin=%lf\n",Xmin);//在屏幕上打印输出Xmin的值
fread(&Ymin,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Ymin里面去,每次读取一个double型字节长度,读取一次
printf("Ymin=%lf\n",Ymin);//在屏幕上打印输出
fread(&Xmax,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Xmax里面去,每次读取一个double型字节长度,读取一次
printf("Xmax=%lf\n",Xmax);//在屏幕上打印输出
fread(&Ymax,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Ymax里面去,每次读取一个double型字节长度,读取一次
printf("Ymax=%lf\n",Ymax);//在屏幕上打印输出
fread(&Zmin,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Zmin里面去,每次读取一个double型字节长度,读取一次
printf("Zmin=%lf\n",Zmin);//在屏幕上打印输出
fread(&Zmax,        sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Zmax里面去,每次读取一个double型字节长度,读取一次
printf("Zmax=%lf\n",Zmax);//在屏幕上打印输出
fread(&Mmin,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Mmin里面去,每次读取一个double型字节长度,读取一次
printf("Mmin=%lf\n",Mmin);//在屏幕上打印输出
fread(&Mmax,         sizeof(double),1,m_ShpFile_fp);//从m_ShpFile_fp里面的值读到Mmax里面去,每次读取一个double型字节长度,读取一次
printf("Mmax=%lf\n",Mmax);//在屏幕上打印输出
// 读取坐标文件头的内容结束
//----------------------------------------------------------------
OnReadLineShp(m_ShpFile_fp);
//getchar();
}