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++;  

  }

}

私の質問は、2次元配列を適切に拡張することですか? それとも別の方法で行うべきですか?

このプログラムは、ファイルから 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';     
}

の後にセミコロンがなく、常に 255char texttexであるため無限ループです。完全な最小限のコンパイル可能なコードを提供してください。他の人があなたのために時間を割いてくれることを期待しているなら、私たちのために時間を割くことから始めるべきです。ありがとうございました。tex

于 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 に答える