Things are looking a bit better now. It still can't find a match for the first word, and the '.', which it replaces with '\0'. No I haven't checked with the debugger. But yes, I will use the debugger to check. That will take a few days to complete.
test.txt = "hello world brown."
dictionary
Code:
how
now
hello
brown
cow
world
purple
reader
output
Code:
hello: no match found
world: matchfound
brown: matchfound
: no match found
Code:
// spellcheck.c - checks spelling of a text file against a dictionary file
// 5 functions: copyfile(), getword(), spellcheck(), options(), editfile()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFERSIZE 4096
#define FILENAME_LENGTH 40
#define STRING_LENGTH 300 // length of string read by fgets() from file
#define WORD_LENGTH 40
void copyfile (void);
char *getword(FILE *tempfile);
char *spellcheck (char *word);
int options(char *errorword);
void editfile();
int main(void)
{
FILE *tempfile;
char *word, *errorword;
copyfile();
tempfile = fopen("temp.txt", "r");
word = malloc(WORD_LENGTH * sizeof(char));
errorword = malloc(WORD_LENGTH * sizeof(char));
word = getword(tempfile);
do
{
if(spellcheck(word) == NULL)
{
printf("%s: match found\n");
}
else
{
errorword = spellcheck(word);
options(errorword);
//editfile();
}
} while (getword(tempfile) != NULL);
return 0;
}
// copy original file to tempfile for reading so that original file can be opened for writing(editing)
void copyfile(void)
{
size_t c;
char buffer[BUFFERSIZE];
FILE *file, *tempfile;
if((file = fopen("test.txt", "r")) == NULL || (tempfile = fopen("temp.txt", "w")) == NULL)
{
perror("Error");
}
while((c = fread(buffer, 1, BUFFERSIZE, file)) > 0)
{
fwrite(buffer, 1, c, tempfile);
}
fclose(file);
fclose(tempfile);
}
char *getword(FILE *tempfile)
{
static char word[WORD_LENGTH]; // static to retain value of word
int ch;
int ctr = 0;
while((ch = fgetc(tempfile)) != EOF)
{
if((ch != ' ') && (ch != '.') && (ch != '-') && (ch != '!') && (ch != '(') && (ch != ')') && (ch != ':') && (ch != ';') && (ch != '\n'))
{
word[ctr] = ch;
ctr++;
}
else
{
word[ctr] = '\0'; // if ch is space or punctuation, add terminator to create string
return word;
}
}
return NULL; // flag that EOF has been reached
}
char *spellcheck(char *word)
{
int x, ch, flag, cmpr, ctr = 0;
char buffer[BUFFERSIZE];
char word_in_dict[WORD_LENGTH];
FILE *dictionary;
dictionary = fopen("dictionary.txt", "r");
while((ch = fgetc(dictionary)) != EOF)
{
if((ch != '\n'))
{
word_in_dict[ctr] = ch;
ctr++;
}
else
{
word_in_dict[ctr] = '\0'; // add terminator to make string
if((cmpr = strcmp(word, word_in_dict)) == 0) // compare word to dictionary word
{
return NULL; // match found, nothing further needs doing
}
ctr = 0; // reset array index to accept new word
}
}
fclose(dictionary);
return word; // if no matches between word and any dictionary words, return word to main()
}
int options(char *errorword)
{
int x, ch;
// do
// {
printf("%s: no match found\n", errorword);
// printf("Alternative words: \n");
// printf("0 to quit \n");
// } while(scanf("%d", &x) > 6);
//
// if(x == 0)
// {
// exit(1);
// }
}
void editfile()
{
}