私はこのコードを持っています:
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++;
}
}