Well, the code is starting to take shape, but I am getting the below errors.
I declared the struct in getword(), and want to pass the struct back to main. How do I create a pointer to the struct in main, and assign the returned struct to it? I tried to do this in line 22, but the struct is local to getword(). Thanks.
Code:
main.c||In function ‘main’:|
main.c|30|error: request for member ‘word’ in something not a structure or union|
main.c|31|error: request for member ‘word’ in something not a structure or union|
main.c|34|warning: comparison between pointer and integer [enabled by default]|
main.c|22|warning: variable ‘word_to_check’ set but not used [-Wunused-but-set-variable]|
main.c|21|warning: variable ‘dictionary’ set but not used [-Wunused-but-set-variable]|
main.c||In function ‘getword’:|
main.c|79|warning: return from incompatible pointer type [enabled by default]|
main.c|79|warning: function returns address of local variable [enabled by default]|
main.c|82|warning: return makes pointer from integer without a cast [enabled by default]|
main.c|64|warning: unused variable ‘x2’ [-Wunused-variable]|
main.c|64|warning: variable ‘x1’ set but not used [-Wunused-but-set-variable]|
||=== Build finished: 2 errors, 8 warnings ===|
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 (FILE *file, FILE *tempfile);
struct word *getword(FILE *tempfile);
void spellcheck (struct word *word_to_check);
void options();
void editfile();
int main(void)
{
FILE *file, *dictionary, *tempfile;
struct word *word_to_check;
copyfile(file, tempfile);
tempfile = fopen("temp.txt", "r");
dictionary = fopen("dictionary.txt", "r");
do {
word_to_check = getword(tempfile);
printf("%s", word_to_check.word);
spellcheck(word_to_check.word);
//option();
//edit();
} while (getword(tempfile) != 1);
return 0;
}
// copy original file to tempfile for reading so that original file can be opened for writing(editing)
void copyfile(FILE *file, FILE *tempfile)
{
size_t c;
char buffer[BUFFERSIZE];
file = fopen("test.txt", "r");
tempfile = fopen("temp.txt", "w");
while((c = fread(buffer, 1, BUFFERSIZE, file)) > 0)
{
fwrite(buffer, 1, c, tempfile);
}
fclose(file);
fclose(tempfile);
}
struct word *getword(FILE *tempfile)
{
struct word {
char word[WORD_LENGTH];
int x;
} word;
int ch;
static int x1 = 0, x2 = 0; // location of space or punctuation
int ctr = 0;
while((ch = fgetc(tempfile)) != EOF)
{
if((ch != ' ') && (ch != '.') && (ch != '-') && (ch != '!') && (ch != '(') && (ch != ')') && (ch != ':') && (ch != ';') && (ch != '\n'))
{
word.word[ctr] = ch;
ctr++;
x1 = ctr;
continue;
}
else
{
word.word[ctr] = '\0'; // if ch is space or punctuation, add terminator to create string
return &word; // return pointer to main()
}
}
return (word.x = 1); // flag that EOF has been reached
}
void spellcheck (struct word *word_to_check)
{
}