【发布时间】:2013-12-06 05:15:00
【问题描述】:
我有一个文本文件中的单词词典,我需要在文本文件中查找某些单词。例如由字母 {q, a, z, w, s, x, e, d, c, r, f, v,t,g,b} 组成的单词或以 {d,o 结尾的单词,我们}。我正在寻找一种可以做到这一点的方法。将所有单词放入数组中最容易吗?还是我应该将其全部保存在文本文件中?我试过文本文件的方法,但被卡住了。这就是我所拥有的。非常感谢!
int size, count;
char *p;
char *words[];
FILE * dict_file;
dict_file = fopen("MyDictionary.txt", "r");
fseek(dict_file, 0, SEEK_END); // seek to end of file
size = ftell(dict_file); // get current file pointer
fseek(dict_file, 0, SEEK_SET); // seek back to beginning of file
// proceed with allocating memory and reading the file
p = dictionary;
while (p = fgets(p, size, dict_file))
{
p += strlen(p);
words[count] = p;
count++;
}
【问题讨论】:
-
你能展示你尝试的文本文件方法的代码吗?目前尚不清楚您是否被困在搜索符合特定条件的单词(如您的问题所暗示的那样)或基本文件阅读(如您的代码所暗示的那样)。
-
我被困在如何搜索单词上。在我上面的代码中,我找到了我的 txt 文件的长度。我不确定如何通过算法找到可以用字母创建的单词。
标签: c dictionary spell-checking