gpt4 book ai didi

c - 从文本文件读取和分析房间

转载 作者:行者123 更新时间:2023-11-30 21:39:52 25 4
gpt4 key购买 nike

我的程序崩溃了,而且我找不到崩溃的位置。我尝试在几乎每一行上使用 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;
}

可能导致崩溃的原因是什么?如何解决?

最佳答案

readLine 函数看起来确实有点容易出错:

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);
}
// The test on the next line is not necessary: it will be caught
// by the first run of the following while loop.
if (ch != '\n')
while (ch != '\n')
ch = getc(fin);
buffer[i] = '\0';
// Allocate a region of memory of (probably) i+1 bytes
l = malloc((i+1) * sizeof(char));
// j will range from zero to i, terminating when j = i+1
for (j = 0; j <= i; j++)
l[j] = buffer[j];
// j now equals i+1 and l[j] is one beyond the size of the memory allocated at l.
l[j] = '\0';

return l;

}

通过更改循环条件来解决此问题

    // j will range from zero to i-1, terminating when j = i
for (j = 0; j < i; j++)
l[j] = buffer[j];
// j now equals i and l[j] is the last element of the memory allocated at l.
l[j] = '\0';

注释:

1) 您应该始终检查 malloc 的返回值。

2) malloc 分配的实际内存大小可能会大于请求的大小,具体取决于堆实现、处理器架构和实际使用的库函数。

此外,您应该对使用 malloc 分配的每个指针调用 free。当进程终止时(如果它运行在相当常见的操作系统中),内存最终会被清理,但最好进行内务管理,特别是在 readLine 返回的那些临时缓冲区上。

您可能还想查看 readRooms 中的 if(rp > MAX) 行,看看是否会导致第 11 个房间的溢出。

关于c - 从文本文件读取和分析房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27270335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com