私はいくつかのファイルからデータを読み込み、それぞれに単語リストを含んでいます。私は各ファイルに単語の数を表示しようとしていますが、私は問題にぶつかっています。たとえば、コードを実行すると、次のような出力が得られます。予期せぬ出力 - 2次元配列をC言語で格納
単語が数千含まれている2つのファイルを除いて、ほぼすべての金額が正しく表示されます。他のすべてのファイルは3桁の単語しか持っていません。
私はこの問題が(どこかに十分なスペースが割り当てられていないのでしょうか)推測できますが、解決方法はわかりません。もしこれがすべて言葉に乏しいのであれば、私はお詫び申し上げます。私の脳は揚げられ、私は苦労しています。どんな助けもありがとう。
私は、私のサンプルコードを可能な限り簡潔に保つようにしました。私は、フル・プログラムに関連した多くのエラー・チェックやその他の作業を省略しました。私はできる限りコメントを追加しました。ありがとう。
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
通常のテキストエディタでファイルを正しく表示できることを確認しましたか?おそらくエンコードの問題でしょうか?改行文字を含めて、すべての単語が60バイト未満であることを確認していますか(「バイト」と「文字」ではないことに注意してください)。これは、ファイルが正しく開かれたことを前提としています( 'entry'は' NULL'ではありません)。フィールドは 'mallocを呼び出した時にメモリにある値で初期化されずに残す代わりに、 'fgets()'ループはほとんどスキップされます(そして 'ferror(entry)'は非ゼロ値を返します)。 –
ファイルは標準のテキストエディタで開くことができます。すべてのファイルはUTF-8です。 finnish.txtの中で最も長い単語は "toimitusjohtaja"で、60バイトの近くではありませんが、私はそれを200に上げた場合に備えて、まったく同じ出力を受け取りました。私は入力を押すまで、一度に1つずつ読み込まれるごとに各単語を印刷する行を挿入し、ファイルは最後まで適切に読み込まれていますが、何らかの理由でカウントが間違っています。 –