我有以下代码:ç简单的拼写检查
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv){
char c, word[100], dictionary[100];
int h,k = 0, l = 0, size, i, right = 11, counter = 0;
char letter[2] = {0};
FILE * file = fopen(argv[1], "r");
FILE * dictionaryFile = fopen("american", "r");
printf("Misspelled words in %s\n", argv[1]);
while(fscanf(file, "%s", word) != EOF){
counter = 0;
k = 0;
while(fscanf(dictionaryFile, "%s", dictionary) != EOF){
l = 0;
for(i = 0; i < strlen(word) - counter; i++){
if(ispunct(word[i])){
counter++;
}
} //here
while(word[k]){
word[k] = tolower(word[k]);
k++;
}
while(dictionary[l]){
dictionary[l] = tolower(dictionary[l]);
l++;
}
size = strlen(word) - counter;
word[size] = '\0';
right = strcmp(word, dictionary);
if(right > 0 || right < 0){
if(word[size - 1] == 's' || word[size - 1] == 'S'){
word[size - 1] = '\0';
right = strcmp(word, dictionary);
if(right > 0 || right < 0){
} else {
counter++;
}
}
}else{
counter++;
}
}
if(counter == 0){
printf("%s\n", word);
}
}
fclose(dictionaryFile);
fclose(file);
return 1;
}
我以一个命令行参数,它是我的文件,句子或单词进行检查。然后我检查它们是否是一个名为'american'的文件,它是一个字典文件。我知道可能有不少错误,我可以弄清楚,我遇到的问题是分段错误,因为它会得到文件的第二个字。我测试了fscanf,它显示了每个由空格分隔的单词,并且正确地做了它,但现在我在第一个单词之后出现了seg错误。我只是一个简单的测试文件,说
hello tsting this checkr tests test.
我从hello到tsting时遇到seg错误。正如我所说,我几乎可以保证修复seg错误代码仍然会有错误,在这一点上,我可以处理我只需要克服这个seg错误的错误。在我在else语句中添加counter ++之前,我没有发现seg错误。我需要他们,我不明白他们为什么会造成错误。
谢谢,我添加了一个新的计数器,并在代码中进行了更改,它的工作原理,我现在只需要获取正确的输出。 – AndyPet74 2015-04-06 02:35:39