我的一个同学给我做的,还有一个有点问题。
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* For _MAX_PATH definition */
#include <malloc.h>
struct Arc
{
int ARC_ID;
char ARC_NAME[50];
int ARC_POINT;
float * pData;
struct Arc * pNext;
};
int main(int argc,char* argv[])
{
FILE * pf;
int i,ArcNum=0;
struct Arc * m_pArc=NULL;
struct Arc * p1,* p2=NULL;
int Len=sizeof(struct Arc);
float * pPoint;
pf=fopen("test.txt","rt");
if(pf==NULL)
{
printf("Open File Fail");
return 0;
}
/* Set pointer to begin
ning of file: */
fseek(pf, 0L, SEEK_SET );
/* Read data back from file: */
while(!feof(pf))
{
p1=(struct Arc *)malloc(Len);
fscanf(pf, "%d,%s,%d/n",&p1->ARC_ID,p1->ARC_NAME,&p1->ARC_POINT);
pPoint=(float *)calloc(p1->ARC_POINT*2,sizeof(float));
for(i=0;i<p1->ARC_POINT;i++)
{
fscanf(pf,"%f,%f/n",&pPoint[2*i],&pPoint[2*i+1]);
}
p1->pData=pPoint;
++ArcNum;
if (ArcNum==1)
{
m_pArc=p2=p1;
m_pArc->pNext=NULL;
}
else
{
p2->pNext=p1;
p2=p1;
}
}
fclose(pf);
/*write data to file*/
pf=fopen("newtest.txt","a+");
if(pf==NULL)
{
printf("Open File Fail");
return 0;
}
p1=m_pArc;
m_pArc=p2=NULL;
while(p1!=NULL)
{
fprintf(pf,"%d,%s,%d/n",p1->ARC_ID,p1->ARC_NAME,p1->ARC_POINT);
for(i=0;i<p1->ARC_POINT;i++)
{
fprintf(pf,"%f,%f/n",p1->pData[2*i]+100000.0,p1->pData[2*i+1]-10000.0);
}
free(p1->pData);
p1->pData=NULL;
p2=p1;
p1=p1->pNext;
free(p2);
}
return 0;
}