0

我有这个代码:

FILE *fr,*fr2,*fr3; 
fr = fopen("med.txt","r");
fr2 = fopen("moje.txt","w");
fr3 = fopen("zaloha.txt","rw");

int  pRadku, nc, inword, pMezery;
int pSlov = 0;
inword =  0;
pRadku = 1;
inword = 0;  
int radky = 20;
int sloupce = 50;
nc = pMezery = 0;
int pocitadlo = 0;
int pocitadlo2 = 0;
int i,j;
char c;

int tex = 255;

char text; 
for(i= 0; i<tex ;i++){
     text[i]='\0';     
}  

 char **pole;
 int pocet = 1000;
 char *p_pom1, *p_pom2, **p_nove;
 pole = (char **)malloc(pocet * sizeof(char));
 for(i=0; i<pocet; i++){
      pole[i] = (char*)malloc(tex * sizeof(char));    
 }





while(( c=fgetc(fr)) != EOF){


        ++nc;
    if(c == '\n'){
        ++pRadku;
            }
    if(c ==' '){
         pMezery++;
             }
    if(c == ' ' || c == '\n' || c == '\t'){
        inword = 0;
    }else if(inword == 0){
            inword = 1;
            ++pSlov;            
    }



  if(pocitadlo >= (pocet-1)){

         int pomPocet = pocet;      
         pocet+=1000;
         pole = (char **)realloc(pole, pocet * sizeof(char));

  }



  if((c != ' ')){
      if(c != '\0'){
           if(c != '\n'){
                if(c != '\t'){
                     if(c != '.'){
                        if(c != ','){
                          text[pocitadlo2]=c;
                          pocitadlo2++;
                        }  
                     }
                }
           }
      }
  }





  if((c == ' ')){
      text[pocitadlo2] = '\0';
      for(i=0;i<tex;i++){
        pole[pocitadlo][i] = text[i];
      }
      for(i=0;i<tex;i++){
        text[i]='\0';
      }
      pocitadlo2=0;
      pocitadlo++;  

  }else if(c == '\0'){
     text[pocitadlo2] = '\0';
      for(i=0;i<tex;i++){
        pole[pocitadlo][i] = text[i];
      }
      for(i=0;i<tex;i++){
        text[i]='\0';
      }
      pocitadlo2=0;
      pocitadlo++;  

  }else if(c == '\n'){
     text[pocitadlo2] = '\0';
      for(i=0;i<tex;i++){
        pole[pocitadlo][i] = text[i];
      }
      for(i=0;i<tex;i++){
        text[i]='\0';
      }
      pocitadlo2=0;
      pocitadlo++;  

  }else if(c == '.'){
      text[pocitadlo2] = '\0';
      for(i=0;i<tex;i++){
        pole[pocitadlo][i] = text[i];
      }
      for(i=0;i<tex;i++){
        text[i]='\0';
      }
      pocitadlo2=0;
      pocitadlo++;  

  }else if(c == ','){
      text[pocitadlo2] = '\0';
      for(i=0;i<tex;i++){
        pole[pocitadlo][i] = text[i];
      }
      for(i=0;i<tex;i++){
        text[i]='\0';
      }
      pocitadlo2=0;
      pocitadlo++;  

  }

}

我的问题是我是否正确扩展了我的二维数组?还是应该以不同的方式来做?

该程序从一个文件中读取 300 000 个单词并将它们保存到一个数组中。

谢谢你的时间。

编辑**

int row = 5;
int column = 5;
int countr = 0;
int countr2 = 0;

int c;
int **array;

array = (int **)malloc(size * sizeof(int*));
 for(i=0; i<row; i++){
    pole[i] = (int*)malloc(column * sizeof(int*));    
}

while(read letters form file (c=fgetc()) != EOF{

if(coutr>=(size-1)){
    row+=10;
     array = (int **)realloc(array, row * sizeof(int*));
}

array[countr][countr2] = c;
countr2++;
if(c == '\0' || c == ' '){
    countr2=0;
    countr++;
}
}
4

2 回答 2

2

这段代码被破坏了,以至于无法诊断出问题所在。举个例子,

int tex = 255;

char texttex 
for(i= 0; tex ;i++){
     text[i]='\0';     
}

后面缺少分号char texttex并且是无限循环,因为tex始终是 255。请提供完整的最小可编译代码。如果您希望别人为您花时间,您应该先花一些时间为我们服务。谢谢你。

于 2012-12-31T11:07:54.720 回答
2

错误的大小被传递给 malloc 函数: sizeof(char) 应该是 sizeof(char*)

pole = (char **)malloc(pocet * sizeof(*char));    

sizeof(char) != sizeof(char*)并且不要对 malloc() 使用类型转换。

我可以在您的代码中看到类似的错误realloc

错误在: pole = (char **)realloc(pole, pocet * sizeof(char));

也是一个语法错误,;缺少:

字符文本

于 2012-12-31T10:40:57.993 回答