Compare two text files - spellchecking program in C

故事扮演 提交于 2019-12-17 21:23:36

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/8512261/compare-two-text-files-spellchecking-program-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!