求教呀。是不是分配空间的问题。
编译过了,但运行的时候出错,帮忙看一下,谢啦。还有就是,再次调用m->vexs[i]->name或者其他时,就出现乱码,有没有什么方法解决啊。#define N 10
#define line 30
typedef struct {
char *name;
char *introduction;
char *location;
}place;
typedef struct {
place *vexs[N];
int arcs[N][N];
}map;
void main(){
map *m=(map*)malloc(sizeof(map));
CreatMap(m);
}
map *CreatMap(map *m) {
FILE *fp;
char *buf, *p;
int k=0,i=0,j=0;
fp=fopen("景点.txt", "r");
buf=(char*)malloc(line*sizeof(char)); // buf用来存放读进来的字符串
while(1){
p=readdata(fp, buf);//每次调用文件指针fp会自动后移一行
if(!p)//文件读取结束则跳出循环
break;
Input(buf,m,k);
k++;
}
free(buf);
}
char * readdata(FILE *fp, char *buf){
return fgets(buf,line, fp);//读取一行到buf
}
void Input(char *buf,map *m,int k){
int i,j;
j=k%3;
for (i=0;i < N;i++){
m->vexs[i]=(place*)malloc(sizeof(place));
switch(j){
case 0:m->vexs[i]->name=(char*)malloc(line*sizeof(char));m->vexs[i]->name=buf;break;
case 1:m->vexs[i]->introduction=(char*)malloc(line*sizeof(char));m->vexs[i]->introduction=buf;break;
case 2:m->vexs[i]->location=(char*)malloc(line*sizeof(char));m->vexs[i]->location=buf;break;
};
}
}