c - 尝试将文本文件加载到数组中,出现段错误。有什么想法吗?

标签 c arrays segmentation-fault

这个函数的任务相当简单。给定一个 char* 数组、一个指向文件的指针和最大字大小,它会读取该文件并将每个字逐个复制到 char* 数组中。由于文件中每行一个单词,因此使用 \n 作为单词之间的分隔符是有意义的。因此,考虑到这一点,代码应该相当容易解释:

void loadDictionary(char* strDictionary[], FILE* filePointer, int nMaxLetters)
{
    int nNameCount= 0, nCursor = 0;
    char* strCurrent;
    char cCurrent;

    //allocate space for a word
    strCurrent = malloc(nMaxLetters * sizeof(char));

    while ((cCurrent = fgetc(filePointer)) != EOF) {

        if(cCurrent != '\n')
        {
            strCurrent[nCursor] = cCurrent;
            nCursor++;

        } else { //then we've reached the end of the line (word)

            //add null termination to string
            strCurrent[nCursor] = '\0'; //SEG FAULT

            //copy string to dictionary
            memcpy(strDictionary[nNameCount], strCurrent, strlen(strCurrent)+1);

            //increment count
            nNameCount++;

            //reset the cursor
            nCursor = 0;
        }
    }
}

此代码在我调用 strCurrent[nCursor] = '\0'; 的行处生成段错误。我不知道为什么,因为从表面上看,这个操作似乎应该与另一个 block 中的操作没有什么不同,我在另一个 block 中调用了 strCurrent[nCursor] = cCurrent;。 strCurrent 应该分配足够的空间来存储所有必需的字符。所以,我有些不知所措。伙计们,帮我解决这个问题。

注意:我认为使用 fgets 而不是 fgetc 来完成此任务可能会更容易。我可能会转向那个;但是,由于我遇到了一个我不理解的错误,因此在理解它之前我不想不管它。

编辑:

有人指出,该错误可能发生在memcpy操作中,可能是由于strDictionary分配不当造成的。这是分配 strDictionarymain block 。也许我犯了一个错误:

int main(int argc, char* argv[])
{
    char** strDictionary;
    FILE* filePointer;
    int nResults = 0, nLines = 0, nNumLines, nMaxChars, i;

    filePointer = fopen("dictionary.txt", "r");

    //obtain the number of lines and the maximum word size of the dictionary
    countLines(filePointer, &nNumLines, &nMaxChars);

    //allocate memory for strDictionary
    strDictionary = malloc(nNumLines * nMaxChars * sizeof(char));
    printf("%d words in dictionary. Longest word is %d letters\n",
            nNumLines, nMaxChars);
    //Output here correctly prints: 1000 and 21

    //reset the file pointer (not sure if this is a necessary step, but oh well)
    filePointer = fopen("dictionary.txt", "r");

    //load dictionary into memory
    loadDictionary(strDictionary, filePointer, nMaxChars);
    for (i=0; i<10; i++)
        printf("%dth element of dictionary: %s\n", i, strDictionary[i]);

    return 0;
}

编辑2:

好的,我决定使用 fgets() 而不是 fgetc() 来大大简化我的功能。我还为 strDictionary 完成了我认为正确的 malloc() 操作。但是,我仍然遇到段错误。这是更新后的代码:

void loadDictionary(char* strDictionary[], FILE* filePointer, int nMaxLetters)
{
    printf("Call to loadDictionary. nMaxLetters = %d\n", nMaxLetters);
    int nWordCount= 0, nCursor = 0;
    char* strCurrent;
    char cCurrent;

    strCurrent = malloc(nMaxLetters); //allocate space for a word


    while (fgets(strCurrent, nMaxLetters, filePointer) != NULL)
    {
        memcpy(strDictionary[nWordCount], strCurrent, strlen(strCurrent)+1);
        nWordCount++;
    }
}

int main(int argc, char* argv[])
{
    char** strDictionary;
    FILE* filePointer;
    int nResults = 0, nLines = 0, nNumLines, nMaxChars, i;
    filePointer = fopen("dictionary.txt", "r");

    //count the lines in the file (works fine)
    countLines(filePointer, &nNumLines, &nMaxChars);

    //allocate space for the dictionary
    strDictionary = malloc(nNumLines * sizeof(char*));
    for (i = 0; i<nLines; i++)
        strDictionary[i] = malloc(nMaxChars * sizeof(char));
    printf("%d words in dictionary. Longest word is %d letters\n",
            nNumLines, nMaxChars);

    //load dictionary into array
    filePointer = fopen("dictionary.txt", "r");
    loadDictionary(strDictionary, filePointer, nMaxChars);
    for (i=0; i<10; i++)
        printf("%dth element of dictionary: %s\n", i, strDictionary[i]);

    return 0;
}

最佳答案

这里:

char cCurrent;
...
while ((cCurrent = fgetc(filePointer)) != EOF) {

您正在截断fgetc()类型 int 的值至char 。这可能会导致 while 条件无法正确识别 EOFcCurrent必须是int .

这里:

//allocate space for a word
strCurrent = malloc(nMaxLetters * sizeof(char));

nMaxLetters必须考虑一个表示字符串 NUL 终止符的额外字符。是否已计算在内?

顺便说一句,sizeof(char)始终为 1。

现在,这个参数声明:

char* strDictionary[]

相当于:

char** strDictionary

或者,IOW,一个指向char的指针的指针。这是因为在 C 中,数组永远不会作为参数传递,只有指向其第一个元素的指针,尽管括号中的欺骗性语法表明某物是数组。

这一行:

memcpy(strDictionary[nNameCount], strCurrent, strlen(strCurrent)+1);

将采取nNameCount 'th 指向 char 的指针并在其指向的地方写入字符数据。

但是调用函数是否分配至少与文件中的行一样多的字符串缓冲区(长度 nMaxLetters )?在将此数组传递到 loadDictionary() 之前,它是否会使用指向这些缓冲区的指针填充一些指向 char 的指针数组。 ? IOW,这段代码期望调用者做这样的事情:

#define nMaxEntries 1000

char* dictionary[nMaxEntries];
int i;
FILE* f;

...

for (i = 0; i < nMaxEntries; i++)
  dictionary[i] = malloc(nMaxLetters);

loadDictionary(dictionary, f, nMaxLetters);

上面的代码中必须检查内存分配失败。另外,我强烈建议通过 nMaxEntries进入或在 loadDictionary() 中使用它因此,如果文件的行数超过 nMaxEntries ,则不会溢出指针数组。 。 nNameCount不应增长超过 nMaxEntries .

更新更新的问题...

这里:

char** strDictionary;
...
strDictionary = malloc(nNumLines * nMaxChars * sizeof(char));

您没有创建指向 char 的指针数组如loadDictionary()根据上述分析,您将创建一个 char 的二维数组。因此,段错误很可能不会发生在这一行:

 strCurrent[nCursor] = '\0'; //SEG FAULT

但在下一个,这在调试器中可能不明显,直到放大并查看代码的反汇编:

 //copy string to dictionary
 memcpy(strDictionary[nNameCount], strCurrent, strlen(strCurrent)+1);

更新2:

我不明白为什么你现在为 nNumLines 分配空间指针:

strDictionary = malloc(nNumLines * sizeof(char*));

但其中nNumLines您初始化的指针 nLines指针(如果我正确读取你的最新代码, nLines 永远不会变成 0 以外的任何值):

for (i = 0; i<nLines; i++)
    strDictionary[i] = malloc(nMaxChars * sizeof(char));

有什么技巧吗?打字错误?

关于c - 尝试将文本文件加载到数组中,出现段错误。有什么想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12828080/

相关文章:

c - 如何从空 vector 中删除内存方向

c - 如何将文件指针移动到文件的下一行?

regex - 在 bash 中,如何检查数组中字符串的部分内容?

javascript - map 函数如何使用其 3 个参数来反转数组中值的顺序

c - 如何 - 使用文件锁求矩阵元素之和 (unix - C/C++)

c - 如何删除 C 中的标志?

arrays - tensorflow : Get indices of array rows which are zero

c++ - string::string 构造函数中的奇怪 "Bus error"

c - 大小 8 的无效读取 .. by

c - 功能结束时出现段错误