Hi all.
If "test.txt" contains "Hello world", and this is copied into "temp.txt", then why does puts(word) result in "hhhhh"? Why not "Hello"? Thanks in advance.
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);
void getword(FILE *tempfile);
void spellcheck (char *word);
void options();
void edit_file();
int main(void)
{
FILE *file, *dictionary, *tempfile;
char word_to_check[WORD_LENGTH];
file = fopen("test.txt", "r");
tempfile = fopen("temp.txt", "w");
copyfile(file, tempfile);
tempfile = fopen("temp.txt", "r");
getword(tempfile);
//dictionary = fopen("dictionary.txt", "r");
//spellcheck(word_to_check);
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];
while((c = fread(buffer, 1, BUFFERSIZE, file)) > 0)
{
fwrite(buffer, 1, c, tempfile);
}
fclose(file);
fclose(tempfile);
}
void getword(FILE *tempfile)
{
char word[WORD_LENGTH];
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[ctr] = 'ch';
ctr++;
continue;
}
else
{
word[ctr] = '\0'; // if ch is space or punctuation, add terminator to create string
puts(word);
exit (1);
}
}
}
//void spellcheck (char *word)
//{
//
//}