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

如何修复用C编写的代码?,c,arrays,text-files,C,Arrays,Text Files,对于20-20的桌子,它工作得非常好,但对于其他任何东西,它都会崩溃。 该文件位于以下链接中:。 我想不出问题出在哪里,因为当我放一张20-20的桌子时,它能工作,什么时候 我给出了一个21-20的表,它做得非常好,但在返回0;之前崩溃了 #include <stdio.h> #include <stdlib.h> int height, length; char **world, **new_world; int load_world_from_file(char

对于20-20的桌子,它工作得非常好,但对于其他任何东西,它都会崩溃。 该文件位于以下链接中:。 我想不出问题出在哪里,因为当我放一张20-20的桌子时,它能工作,什么时候 我给出了一个21-20的表,它做得非常好,但在返回0;之前崩溃了

#include <stdio.h>
#include <stdlib.h>

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;
}
#包括
#包括
int高度、长度;
字符**世界,**新世界;
int从_文件加载_world_(字符文件名[]){
int i,j;
char ch;
文件*fp;
fp=fopen(文件名,“r”);
如果(fp==NULL){
perror(“打开文件时出错。\n”);
退出(退出失败);
}
fscanf(fp、%d%d、&高度和长度);
printf(“此处:%d,%d\n”,高度,长度);
/*迪洛西·杜纳米卡·图皮纳卡·卡塔克索里斯*/
/*表项下的声明*/
world=(char**)malloc(sizeof(char*)*(height+1));/*height+1('\0'))*/
对于(i=0;i<2*高度;i++){
/*高度+高度(“|”)+1(“\0”)*/
world[i]=(char*)malloc(sizeof(char)*(length+length+1));
}
/*高度+1(“\0”)*/
新世界=(char**)malloc(sizeof(char*)*(高度+1));
对于(i=0;i<2*高度;i++){
/*高度+高度(“|”)+1(“\0”)*/
新世界[i]=(char*)malloc(sizeof(char)*(length+length+1));
}
printf(“文件%s的内容为:\n”,文件名);
i=0;
j=0;
而((ch=fgetc(fp))!=EOF){
i++;
如果(i=长度+长度+1){
j++;
i=0;
}
世界[j]=ch;
printf(“%c”,世界[j]);
}
printf(“\n”);
对于(i=0;i<((2*长度)+2*高度)+1;i++){
printf(“%c”,世界[i]);
}
fclose(fp);
返回0;
}
int main(){
char filename[30],filename2[30];
printf(“输入要打开的文件名:\n”);
scanf(“%s”和文件名);
从_文件(文件名)加载_world_;
返回0;
}
非常感谢你的帮助。

i=length+length+1
应该使用
=
。否则,
j
将比预期的要快得多。谢谢。这很有趣,因为这是一个语法错误,我修复了,但问题不起作用!它与一些甚至没有意义的东西一起工作:如果(i=length+length+1){
world[index](world[j],world[j])
char*
。错误的
world[j]=ch;printf(“%c”,world[j]);
表示(i=0);意思是
%c
表示
char但
world[j]
类型为
char*
!而
%s
将不会有帮助,因为您没有在每行末尾添加空字符或使用空字符初始化。