我的程序崩潰,無法找到位置。我幾乎在每一行嘗試使用printf進行調試,但我無法找到問題所在。 我認爲它可能在readLine
函數,但我完全失去了。從文本文件中讀取和分析房間
,我使用的輸入文件是
*HallStudyCellarKitchen*StudyHallGarden*CellarHall*KitchenHallGarden*GardenStudyKitchen
這意味着每個「*」隔開一個新的空間,然後將它顯示了門的那間導致。我的計劃
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define BMAX 100
struct room * rooms[MAX];
int rp; // room count
// struct room - name, array of up to 4 doors, number of doors
struct room {char * name; struct door * doors[4]; int dp;};
// struct door - name for the room it connects to, & a pointer to that room
struct door {char * name; struct room * room;};
struct door * newDoor(char * name){
struct door * d; // pointer d to the address of door
d = (struct door *) malloc(sizeof(struct door));
d->name = name; // name of new door is name
d->room = NULL; // NULL room pointer
return d;
};
struct room * newRoom(char * name){
struct room * r; // pointer r to the address of room
printf("New room is %s\n",name);
r = (struct room *) malloc(sizeof(struct room));
r->name = name; // name of new room is name
r->dp = 0; // no doors
return r;
};
showRoom(struct room * r){
int i;
printf("room name: %s\n", r->name);
for (i = 0; i < (r->dp); r++){
printf("%d %s\n", i,r->doors[i]->name);
}
}
showRooms(){
int i;
for (i = 0; i < rp; i++){
showRoom(rooms[i]);
}
}
char * readLine(FILE * fin){
char buffer[BMAX];
int i,j;
char ch;
char * l;
i = 0;
ch = getc(fin);
if (ch == EOF)
return NULL;
while (ch!='\n' && i < (BMAX - 1)){
buffer[i] = ch;
i++;
ch = getc(fin);
}
if (ch != '\n')
while (ch != '\n')
ch = getc(fin);
buffer[i] = '\0';
l = malloc((i+1) * sizeof(char));
for (j = 0; j <= i; j++)
l[j] = buffer[j];
l[j] = '\0';
return l;
}
readRooms(FILE * fin)
{ char * l;
rp = 0;
// printf("3"); fflush(stdout);
while((l = readLine(fin)) != NULL)
{
if(rp > MAX)
{
printf("it's too many rooms\n");
exit(0);
}
//printf("%s",l);
rooms[rp] = newRoom(l);
//l = readLine(fin);
if (strncmp(l,"*")==0){
//printf("2"); fflush(stdout);
rp++;
}
while(strncmp(l,"*")!=0)
{
//printf("1"); fflush(stdout);
if((rooms[rp] -> dp) > 4)
{ printf("it's too many doors\n");
exit(0);
}
rooms[rp] -> doors[rooms[rp] -> dp] = newDoor(l);
rooms[rp] -> dp++;
l = readLine(fin);
}
//rooms[rp] -> dp = 0;
//rp++;
//l = readLine(fin);
}
}
connect()
{ int i,j,k;
for(i = 0; i < rp; i++)
for(j = 0; j < rooms[i]->dp; j++)
{ for(k = 0; k < rp; k++)
if(strcmp(rooms[k]->name,rooms[i]->doors[j]->name) == 0)
{ rooms[i]->doors[j]->room = rooms[k];
break;
}
if(k == rp)
{ printf("can't find %s\n",rooms[i]->doors[j]->name);
exit(0);
}
}
}
int main(int argc,char ** argv){
FILE * fin;
struct room * r; // current room
// struct door * d;
int d;
if((fin=fopen(argv[1],"r"))==NULL)
{ printf("cannot open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
printf("11"); fflush(stdout);
readRooms(fin);
printf("22");
fclose(fin);
showRooms();
connect();
r = rooms[0];
while(1)
{ showRoom(r);
printf("enter door number> ");
scanf("%d",&d);
if(d >= (r->dp))
printf("bad door number\n");
else
r = r->doors[d]->room;
}
return EXIT_SUCCESS;
}
什麼可能是導致飛機失事的原因,以及如何解決它的
碼?
當它崩潰時,你會得到什麼輸出?如果您可以指向代碼崩潰的部分,這將有所幫助。你的printf應該引導你到什麼地方? – 2014-12-03 11:18:31
這可能是一個很好的時間點,可以停止依靠printf來進行調試並獲得一些實際調試器的使用經驗。 – Bart 2014-12-03 11:19:06
'#include''strcmp' –
2014-12-03 11:22:23