繁体   English   中英

意外的输出-在c中存储到2D数组中

[英]Unexpected Output - Storing into 2D array in c

我正在从许多文件中读取数据,每个文件都包含一个单词列表。 我试图显示每个文件中的单词数,但是遇到了问题。 例如,当我运行代码时,收到如下所示的输出。

除了两个文件(每个文件包含成千上万的字数)外,几乎所有金额都可以正确显示。 每个其他文件仅包含三位数的单词,它们看起来还不错。

我只能猜测这个问题可能是什么(某处没有足够的空间分配?),我也不知道如何解决。 如果这句话措辞不好,我深表歉意。 我的大脑炸了,我在挣扎。 任何帮助,将不胜感激。

我试图使示例代码尽可能简短。 我省去了很多错误检查和其他与整个程序有关的任务。 我还在可能的地方添加了评论。 谢谢。

StopWords.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <stddef.h>
#include <string.h>

typedef struct
{
    char stopwords[2000][60];
    int wordcount;
} LangData;

typedef struct
{
    int languageCount;
    LangData languages[];
} AllData;


main(int argc, char **argv)
{
    //Initialize data structures and open path directory
    int langCount = 0;
    DIR *d;
    struct dirent *ep;
    d = opendir(argv[1]);

    //Count the number of language files in the directory
    while(readdir(d))
        langCount++;

    //Account for "." and ".." in directory
    //langCount = langCount - 2 THIS MAKES SENSE RIGHT?
    langCount = langCount + 1; //The program crashes if I don't do this, which doesn't make sense to me.

    //Allocate space in AllData for languageCount
    AllData *data = malloc(sizeof(AllData) + sizeof(LangData)*langCount); //Unsure? Seems to work.

    //Reset the directory in preparation for reading data
    rewinddir(d);

    //Copy all words into respective arrays.
    char word[60];
    int i = 0;
    int k = 0;
    int j = 0;
    while((ep = readdir(d)) != NULL) //Probably could've used for loops to make this cleaner. Oh well.
    {
        if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
        {
            //Filtering "." and ".."
        }
        else
        {
            FILE *entry;

            //Get string for path (i should make this a function)
            char fullpath[100];
            strcpy(fullpath, path);
            strcat(fullpath, "\\");
            strcat(fullpath, ep->d_name);

            entry = fopen(fullpath, "r");

            //Read all words from file
            while(fgets(word, 60, entry) != NULL)
            {
                j = 0;

                //Store each word one character at a time (better way?) 
                while(word[j] != '\0') //Check for end of word
                {
                    data->languages[i].stopwords[k][j] = word[j];
                    j++; //Move onto next character
                }
                k++; //Move onto next word
                data->languages[i].wordcount++;
            }

            //Display number of words in file
            printf("%d\n", data->languages[i].wordcount);
            i++; Increment index in preparation for next language file.

            fclose(entry);
        }
    }
}

输出量

256 //czech.txt: Correct
101 //danish.txt: Correct
101 //dutch.txt: Correct
547 //english.txt: Correct
1835363006 //finnish.txt: Should be 1337. Of course it's 1337.
436 //french.txt: Correct
576 //german.txt: Correct
737 //hungarian.txt: Correct
683853 //icelandic.txt: Should be 1000.
399 //italian.txt: Correct
172 //norwegian.txt: Correct
269 //polish.txt: Correct
437 //portugese.txt: Correct
282 //romanian.txt: Correct
472 //spanish.txt: Correct
386 //swedish.txt: Correct
209 //turkish.txt: Correct 

文件中的单词超过2000个吗? 您仅分配了2000个单词的空间,因此一旦程序尝试在单词2001上进行复制,它将在为该数组分配的内存之外执行该操作,可能会分配到为“ wordcount”分配的空间中。

我还要指出的是,fgets返回一个字符串到行尾或最多n个字符(在您的情况下为60个字符),以先到者为准。 如果正在读取的文件中每行只有一个单词,这将起作用,否则将不得不在字符串中定位空格并从那里计算单词。

如果您只是想获取单词数,则无需首先将所有单词存储在数组中。 假设每行一个单词,则以下内容也应同样有效:

 char word[60];
 while(fgets(word, 60, entry) != NULL)
        {
            data->languages[i].wordcount++;
        }

与fgets参比http://www.cplusplus.com/reference/cstdio/

更新我又看了一眼,您可能想要按以下方式尝试分配数据:

typedef struct
{
    char stopwords[2000][60];
    int wordcount;
} LangData;

typedef struct
{
    int languageCount;
    LangData *languages;
} AllData;

AllData *data = malloc(sizeof(AllData));
data->languages = malloc(sizeof(LangData)*langCount);

这样,将为语言数组专门分配内存。

我同意langCount = langCount-2是有意义的。 你遇到了什么错误?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM