遇到的问题是:
int readrule(char rulepath[],struct rule *head)
{char buf[100];
FILE *fp;
int n=0;
struct rule *p1,*p2;
p1=(struct rule *)malloc(sizeof(struct rule));
if( (fp=fopen(rulepath,"r"))==NULL)
return(n);
while(fgets(buf,100,fp)!=NULL)
{
n++;
if(n == 1)
{
head->next = p1;
sscanf(buf,"%[^,],%[^,],%[^\r\n]",p1->file,p1->host,p1->dir);
}
else
p2->next=p1;
p2=p1;
p1=(struct rule *)malloc(sizeof(struct rule));
sscanf(buf,"%[^,],%[^,],%[^\r\n]",p1->file,p1->host,p1->dir);
memset(buf,0,sizeof(buf));
}
fclose(fp);
p2->next=p1;
p1->next=NULL;
return(n);
}
两段相同的这样代码,运行一个没问题,运行两个就报错。后来发现是sscanf(buf,"%[^,],%[^,],%[^\r\n]",p1->file,p1->host,p1->dir)这里读入数据使p1->dir超出了数组范围