如何修复用C编写的代码?

它适用于20-20表,但对于其他任何事情它都会崩溃。 该文件位于以下链接: http : //www.csd.uoc.gr/~hy100/glid.txt 。 我无法弄清楚问题是什么,因为当我放一个20-20的表时它起作用,当我给21-20表时,它完全可以完成工作,但在能够返回0之前崩溃; 。

#include  #include  int height, length; char **world, **new_world; int load_world_from_file(char filename[]) { int i, j; char ch; FILE *fp; fp = fopen(filename, "r"); if (fp == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } fscanf(fp, "%d %d", &height, &length); printf("here:%d,%d\n", height, length); /*dilosi dunamika tou pinaka kataxoriseis*/ /* declaration under the table entries */ world = (char **) malloc(sizeof(char *) * (height + 1));/*height + 1 ('\0')*/ for (i = 0; i < 2 * height; i++) { /* height + height ('|') + 1 ('\0') */ world[i] = (char *) malloc(sizeof(char) * (length + length + 1)); } /* height + 1 ('\0') */ new_world = (char **) malloc(sizeof(char *) * (height + 1)); for (i = 0; i < 2 * height; i++) { /* height + height ('|') + 1 ('\0') */ new_world[i] = (char *) malloc(sizeof(char) * (length + length + 1)); } printf("The contents of %s file are :\n", filename); i = 0; j = 0; while ((ch = fgetc(fp)) != EOF) { i++; if (i = length + length + 1) { j++; i = 0; } world[j] = ch; printf("%c", world[j]); } printf("\n"); for (i = 0; i < (((2 * length) + 2) * height) + 1; i++) { printf("%c", world[i]); } fclose(fp); return 0; } int main() { char filename[30], filename2[30]; printf("Enter the name of file you want to open:\n"); scanf("%s", &filename); load_world_from_file(filename); return 0; } 

非常感谢您的帮助。 它