2014-03-03 5 views
1

나는 작업중인 코드에서 45430 단어의 사전을 읽고 각 단어 내에 포함 된 사전의 다른 모든 단어를 파일에 인쇄합니다. 나는 파일 MyDictionary txt 파일을 문자 배열 단어 [45430] [30]로 읽은 다음 단어 - 인 - 워드 txt 파일에 이것을 인쇄하는 중 일하고 있습니다. 내가 그렇게 할 때 나는 44946 단어에서 seg fault로 빠져 나간다. 그러나 같은 while 루프에서 나는 또한 콘솔에 인쇄하고 모든 단어가 제대로 출력된다. 파일에 쓰는 중 왜이 오류가 발생합니까? 그리고 왜 콘솔에 쓰는 오류가 없습니까?사전 내용을 파일에 쓸 때 세그먼트 오류가 발생했습니다.

코드 : 당신은 간접 너무 많은 수준을 가지고

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

가되다 :

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

내 과제에는 실제로 단어의 형식이 필요합니다. 단어 [num_words_in_dict] [length_longest]. 나는이 아이디어가 단어 안에있는 단어를 찾는 것이 더 쉬울 것이라고 생각합니다. 일단 단어를 모두 파일로 인쇄하는 법을 배웁니다. 고마워요 – user2985363

+0

맞습니다. 그러나'strcpy()'는 목적지보다 10 바이트 더 짧기 때문에 대상을 오버플로 할 수 있습니다. – alk

0

'단어'포인터의 배열이되어야한다, 그래서 권리를 형식이 char **이면이 아닙니다..

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

나는 너와 나에게 완벽한 의미를 갖는 Johns 솔루션을 사용할 수 있기를 바란다. 그러나 단어의 형식이 필요합니다. [num_words_in_dict] [length_longest] – user2985363

+0

당신은 모든 사전이 포함 된 메모리 블록 하나를 갖고 싶습니까? 왜 문자열 배열이 적합하지 않습니까? –

+0

나는 단어의 각 문자를 따라 가면서 나중에 단어 안에서 단어를 찾으려고 할 수 있다고 믿는다. 나는 그렇지 않으면 그것을 할 수 있지만 명령은 명확하게 그렇지 않다는 것을 안다. – user2985363

0

는 다음과 같이 할당 할 메모리의 한 덩어리를하려면

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 
} 

그런 다음 파일에서 읽기는 다음과 같이 수행 할 수 있습니다 : 배열의 각 항목은 문자의 버퍼에 대한 포인터 :

#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; 
}