I get the fgets() error when i try to run it.......i dont know if i am
using the command wrong but i posted a portion of my code.
As for truex command......i never used it and don't know how to use it.
You have certainly tried hard enough and I can see you have invested a lot
of time in this. But you ignored my early advice about breaking things down
into pieces. You have one huge function called, roughly, createbinarytree.
That function opens a file, reads from the file, finds the words, and adds
the words to a tree. You need to assign responsibility to a group of
functions that each do one thing and do it well. Eventually, you will need
a menu. Remember I told you to defer that? At this point in the learning
process, a menu is just a time consuming distraction.
I think in terms of plateaus. The first plateau is to construct a tree
containing words, without such a tree none of the other stuff called for can
happen. There are three parts to reaching this first plateau. Opening and
reading a file, isolating the words, and adding the words to a tree. I give
code below that reads the file and isolates the words. I suppose there are
bugs in it, but it passes some cursory checks. Note that isolating the
function like this will make it possible to fairly easily refine the
definition of a word later on, remember that I said this was a first cut at
a definition. The code could be made more compact and cryptic than it is.
Now you need to write a function something like this:
void add_word(struct* Node tree, char* word);
The first parameter identifies the tree and the second identifies the word
to be added. With a modern computer there is no reason to fail so the
return is simply void. The fist entry added will be an "odd-ball" and the
way I would do it would require unique logic for that particular case.
The code below was tested with the file testtree.txt which is attached to a
message in alt.test named "tree tree tree". You will probably recognize the
text of the message. Copy that file to the directory your compiler is using
for the source code you write. My news server does not let me attach files
to posts to this news group.
------------
#include <stdio.h>
#include <ctype.h>
char* fn = "testtree.txt";
// finds next word in line and returns a pointer to it.
// writes '\0' in line, thus destroying original content
// returns NULL if no word could be found.
char* get_word(char* line)
{
char ch;
char* start;
static char* resume;
if(resume)
line = resume; // continue parsing original string
// skip non letters
while(1)
{
if(*line == 0)
{
resume = 0;
return NULL; // end of line
}
ch = *line++;
ch = tolower(ch);
if(islower(ch))
{
start = --line; // undo premature advance
break;
}
}
// have the beginning of a word.
// now find the end of the word
while(1)
{
ch = *line++;
ch = tolower(ch);
if(!islower(ch))
{
// remember where to continue from
resume = line;
// mark this as the end of a C string
line--;
*line = '\0';
// was used for intial testing
//printf("%s\n", start);
return start;
}
} // end while
} // end function
//========================
int main(void)
{
FILE* in;
char buffer[1000];
char* wd;
char* status;
in = fopen(fn, "r");
if(!in)
{
printf("Can't open file\n");
getchar();
return 1;
}
while(1)
{
status = fgets(buffer, 999, in);
if(!status)
{
printf("EOF encountered\n");
getchar();
break;
}
while(wd = get_word(buffer))
printf("%s\n", wd);
}
getchar();
return 0;
}