比较两个文本文件 - 在C程序拼写检查 [英] Compare two text files - spellchecking program in C

查看:75
本文介绍了比较两个文本文件 - 在C程序拼写检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个拼写检查程序将比较用户的文本文件,借助字典,看看他们是否进入了词在字典中。如果不是这样,一个错误信息被打印告诉特定的词是错误的用户。我已经尝试了数低于code的变化,但没有得到预期的效果。这件事情在的投掷出来的嵌套while循环。这code在起草阶段,我必须以使其更高效的内存等整齐起来。我在得到它的第一份工作只是感兴趣。谢谢!

编辑:改变了code稍微按照下面的提示。它现在读取的第一个字,并说,它是在词典中。然后,它显示的第二个字,但字典扫描循环不运行和程序挂起。我知道它的嵌套循环的同时导致了问题我不能让我的头周围!

  / *拼写检查程序* /
/ *作者:* /#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
/ *打开文件,并测试他们打开* /
FILE * FP1;
FILE * FP2;
焦炭FNAME [20];
焦炭wordcheck [45]; / *在英语中最长的单词是45个字母* /
焦炭worddict [45];
烧焦假;
INT I;
INT dictcount = 0;FP1 = FOPEN(dictionary.txt,R);如果(FP1 == NULL)
{
的printf(字典文件没有打开。);
出口(0);
}的printf(请输入您要检查的文件的路径:\\ n);
scanf函数(%S,FNAME);
scanf函数(%C,&安培;假);FP2 = FOPEN(FNAME,R);
    如果(FP2 == NULL)
        {
        的printf(你的文件没有打开,请检查您的文件路径,然后重试\\ n);        的printf(请输入文件路径要检查:\\ n);
        scanf函数(%20多岁,FNAME);        FP2 = FOPEN(FNAME,R);
        }    其他
        {
        的printf(您的文件打开正确的\\ n);
        }/ *当文件打开,读取该文本文件中的每个字到一个数组:* /    而(的fscanf(FP2,%S,wordcheck)!= EOF)//读取从文本文件单词到数组//
    {        对于(i = 0; wordcheck [I];我++)
        {
            wordcheck [I] = tolower的(wordcheck [I]); //使所有字符小写//
        }        的printf(%S,wordcheck);        而(dictcount> = 0)//读取字典中的单词到数组//
        {
            dictcount = 0;
            的fscanf(FP1,%S,worddict);            如果(STRCMP(wordcheck,worddict)== 0)//比较字符串//
            {
            的printf(这个词:%s是在字典中的\\ n,wordcheck);
            打破;
            }            其他
            {
            dictcount ++;
            }            如果(worddict == NULL)
            {
            的printf(你的话:%s不是在字典中的\\ n,wordcheck);
            }
        }
        dictcount ++;
    }
    FCLOSE(FP1);
    FCLOSE(FP2);返回0;
}


解决方案

解决这个的通常的方法是先读字典,并建立一个哈希表。然后你会在从输入文件并标记错误一次读取一个字,如果字不能在哈希表存在。

I'm writing a spellchecking program that will compare a user's text file with a dictionary to see if the words they entered are in the dictionary. If not, an error message is printed to tell the user that the specific word is wrong. I've tried a number of variations of the code below but not getting the desired results. It's something in the nested while loop that's throwing it out. This code is in draft stage I have to make it more memory efficient etc and tidy it up. I'm just interested in getting it working first. Thanks!

EDIT: Have altered the code slightly as per the tips below. It now reads the first word and says that it is in the dictionary. It then displays the second word but the dictionary scanning loop doesn't run and the program hangs. I know its the nested while loop causing the issue I just can't get my head around it!

/*Spellcheck program*/
/*Author: */

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

int main(void)
{
/*Open files and test that they open*/
FILE *fp1;
FILE *fp2;
char fname[20];
char wordcheck[45];/*The longest word in the English Language is 45 letters long*/
char worddict[45];
char dummy;
int i;
int dictcount = 0;

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

if (fp1 == NULL)
{
printf("The dictionary file did not open.");
exit(0);
}

printf("Please enter the path of the file you wish to check:\n");
scanf("%s", fname);
scanf("%c", &dummy);

fp2 = fopen(fname, "r");
    if (fp2 == NULL)
        {
        printf("Your file did not open, please check your filepath and try again.\n");

        printf("Please enter path of file you wish to check: \n");
        scanf("%20s",fname);

        fp2 = fopen(fname, "r");
        }

    else
        {
        printf("Your file opened correctly\n");
        }

/*When files are open, read each word from the text file into an array:*/

    while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//
    {

        for (i=0; wordcheck[i]; i++)
        {
            wordcheck[i] = tolower(wordcheck[i]);//makes all characters lower case//
        }

        printf("%s", wordcheck);

        while(dictcount >= 0)//reads dictionary word into array//
        {   
            dictcount = 0;
            fscanf(fp1,"%s", worddict);

            if(strcmp(wordcheck, worddict)==0)//compare strings//
            {
            printf("This word: %s is in the dictionary\n", wordcheck);
            break;
            }

            else
            {
            dictcount++;
            }

            if(worddict == NULL)
            {
            printf("Your word: %s is not in the dictionary\n", wordcheck);
            }
        }
        dictcount++;
    }   
    fclose(fp1);
    fclose(fp2);

return 0;
}

解决方案

The usual way of solving this is to first read the dictionary and build a hash table. You'd then read one word at a time from the input file and flag an error if the word doesn't exist on the hash table.

这篇关于比较两个文本文件 - 在C程序拼写检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆