Originally Posted by
slingerland3g
I would think it best to edit out that '\n' (newline) from your dictionary. This way your users, or this file, will not have to actually include this with the inserted word to check for spelling, and that would be odd. If you must, then the only way I see it is that you will have to store each word you input into an array in order to then tack on this extra character '\n' and then pass this off to be checked against your dictionary.
I am trying to use strtok() to divide the dictionary file up, so it puts '\0' where there was a '\n'. It works fine but it messes up my words file. The words file is divided by 2 '\n' symbols and there's maybe 5 or 6 words per line. When I run it only compares the first word on each line. It checks for correct spelling, but does not access the rest of the line. Any ideas why that is?
My code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
FILE *fp, *wfp;
} Dict;
int inDict(FILE*, char*);
int main(int argc, char **argv)
{
int size = (1000 * sizeof(int)), i=0;
Dict words;
char *word, *buf;
char *delim = "\n\t\",. ";
words.fp = fopen("/usr/share/dict/words", "r");
words.wfp = fopen(argv[1], "r");
if ((buf = (char*)malloc(1000 * sizeof(char*))) == NULL) {
printf("Allocation error");
}
while (fgets(buf, size, words.wfp)) {
for (word = buf; word; word = NULL) {
word = strtok(buf, delim);
if (inDict(words.fp, word)) {
printf("%s OK\n", word);
}
else {
printf("%s MISSPELLED\n", word);
}
}
}
free(buf);
fclose(words.fp);
fclose(words.wfp);
return 0;
}
int inDict(FILE* d, char* word) {
int len = (1000 * sizeof(int)), i=0;
char *dict, *buffer;
if ((dict = (char*)malloc(1000 * sizeof(char*))) == NULL) {
printf("Allocation error");
}
if ((buffer = (char*)malloc(1000 * sizeof(char*))) == NULL) {
printf("Allocation error");
}
while (fgets(buffer, len, d)) {
for (dict = strtok(buffer, "\n"); dict; dict = strtok(0, "\n")) {
if((strcasecmp(word,dict) == 0)) {
return 1;
}
}
}
rewind(d);
free(dict);
return 0;
}