2014-03-03 34 views
1

我正在處理的代碼讀取45430個字的字典,然後將每個字中包含的字典中的所有其他字打印到文件中。我只是將獲取文件MyDictionary txt文件讀入字符數組字[45430] [30],然後將其打印到單詞txt文件中。當我這樣做時,我在44946字處遇到seg故障,但是在同一循環中,我也正在打印到控制檯,並且所有單詞都能正確打印出來。爲什麼我在寫這個文件時遇到這個seg錯誤?爲什麼沒有seg錯誤寫入控制檯?將字典的內容寫入文件時出現分段錯誤

代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <wchar.h> 
#include <string.h> 
//char ***alloc_array(int,int); 

int main(void){ 
     FILE *fr; //declare file read file pointer 
     FILE *fp; //declare file printed file pointer 

     //char array to read in up to 30 chars 
     char line[31]; 

     long numwords=45430; //number of words in dictionary 
     int maxlength=31; // the longest string in dictionary (30 chars) 
     long i; //counts up to 45430 

     //allocate space for 45430 words at a max length of 30 chars (1 extra char for "\0")  
     char ***word = calloc(numwords, sizeof(char **)); 
     for(i = 0; i != numwords; i++) { 
      word[i] = calloc(maxlength, sizeof(char *)); 
     } 

     //Open MyDictionary.txt and determine if there is an error or not   
     fr = fopen ("MyDictionary.txt", "r"); // open the file for reading  

     if(fr==NULL){ 
       printf("\nError! opening input file"); 
       exit(1); //Program exits if file pointer returns NULL. 
     } 

     //Open words-within-words.txt and determine if there is an error or not   
     fp = fopen ("words-within-words.txt", "w"); // open the file for reading  
     if(fp==NULL){ 
       printf("\nError! opening output file"); 
       exit(1); //Program exits if file pointer returns NULL. 
     } 

     int j=0; //counts to 30 for max length 
     i=0; 
     while(fgets(line, 40, fr) != NULL){ //get a line, up to 40 chars from fr and put first . done if NULL 

      for(j=0;j<30;){ 
       word[i][j]=&line[j]; 
       j++; 
      } 
      j=0; 
      printf("\n%s",word[i][j]); //print out each word of dictionary to console on its own line 
      /* 
      if((i>4 && i<8)||(i>45428)){ 
       fprintf(fp,"\nanalyze:word[i][0]=%s\tword[i][2]=%s\ti=%li",word[i][0],word[i][2],i+1); 
      } 
      */ 
      fprintf(fp,"%s",word[i][j]); //print out each word of dictionary to words-in-words on its own line 
      i++; 
     } 
     fclose(fr); //close the files prior to exiting 
     fclose(fp); 
     return 0; 
} //main 

回答

1
char ***word = calloc(numwords, sizeof(char **)); 
for(i = 0; i != numwords; i++) { 
    word[i] = calloc(maxlength, sizeof(char *)); 
} 

你已經有了一個間接層次太多。您正在存儲單詞列表。一個單詞是char *,所以單詞列表將是char **

char **word = calloc(numwords, sizeof(char *)); 
for (i = 0; i != numwords; i++) { 
    word[i] = calloc(maxlength, sizeof(char)); 
} 

這將需要更改其餘的代碼。你完全可以擺脫j。這:

for(j=0;j<30;){ 
    word[i][j]=&line[j]; 
    j++; 
} 

變爲:

strcpy(word[i], line); 

而且這樣的:

j=0; 
printf("\n%s",word[i][j]); 
fprintf(fp,"%s",word[i][j]); 
i++; 

變爲:

​​
+0

對於我的任務,我實際上需要詞的形式。字[num_words_in_dict] [length_longest]。我相信這個想法是這樣的,一旦我學會了如何將所有文字打印到文件中,就可以更容易地找到單詞中的單詞。謝謝 – user2985363

+0

正確。然而,'strcpy()'可能會溢出目標,因爲它比源短10個字節。 – alk

0

'字' 應該是一個指針數組,所以正確的類型是char **,而不是char ***。 陣列中的每個條目是一個指向字符的緩衝液:

char **word = (char **)calloc(numwords, sizeof(char *)); 
if (!word) 
    // exit with error 

for (i = 0; i != numwords; i++) { 
    word[i] = (char *)calloc(maxlength, sizeof(char)); // just allocate 31 bytes 
    if (!word[i]) 
     // exit with error 
} 

然後從文件的讀可以這樣來完成:

for (i = 0; fgets(line, 40, fr); i++) { 
    strncpy(word[i], line, maxlength); 
    printf("word %d: %s\n", i, word[i]); 
} 
+0

我希望我能使用你和約翰斯的解決方案,他們對我來說非常合適。不過,我需要的單詞格式是[num_words_in_dict] [length_longest] – user2985363

+0

你的意思是,你想擁有一個包含你所有字典的內存塊嗎?爲什麼一串字符串不適合? –

+0

我相信這樣可以稍後通過遍歷單詞的每個字符來嘗試在單詞中找到單詞。我知道我可以這樣做,但指示明確表示否則。 – user2985363

0

爲了對存儲器中的一個塊都分配這樣:

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

int main(void) 
{ 
    int result = EXIT_SUCCESS; 

    size_t n = 45430; 
    size_t l = 30; 

    char (* words)[n][l + 1] = calloc(n, l + 1); 
    if (NULL == words) 
    { 
    result = EXIT_FAILURE; 
    perror("calloc() failed"); 
    goto lblExit; 
    } 

    for (size_t i = 0; i < n; ++i) 
    { 
    strncpy((*words)[i], "test", l); 
    } 

    for (size_t i = 0; i < n; ++i) 
    { 
    printf("%zu: '%s'\n", i, (*words)[i]); 
    } 

    free(words); 

lblExit: 

    return result; 
} 
相關問題