c - 将字典内容写入文件时出现段错误

标签 c segmentation-fault printf dynamic-allocation calloc

我正在处理的代码在一个包含 45430 个单词的字典中读取,然后将每个单词中包含的字典中的所有其他单词打印到文件中。我正在努力将文件 MyDictionary txt 文件读入 char 数组 word[45430][30],然后将其打印到 words-in-words txt 文件中。当我这样做时,我在 44946 字处遇到了段错误,但在同一个 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

最佳答案

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]);

关于c - 将字典内容写入文件时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154651/

相关文章:

c - 在C中添加字段值?

c - 尝试将通信世界拆分为 MPI 中的多个通信世界或 block ?

java - 我认为 printf 和字符串连接;初学者的 future 看起来很黯淡

c - C 中的整数打印奇怪的东西

使用 URLConnection 时出现 java.lang.NegativeArraySizeException

c - 字符数组的右填充在 c 中不起作用

c - 字符串运算符#

c++ - 如何通过 wprintf 函数打印 uint32_t 变量值?

在 C 中找不到错误 - 'segmentation fault'

c++ - 堆分配的 QApplication 被删除