I am trying to read a text file that contains a paragraph and my goal is to put each of the words of the text file into a tree and keep count of how many times each word appears in the file.
All of my libraries have been tested and there seems to be no problem and I read each one of the words one by one from the file with no problem.
The problem arises when I try to send the word from the file(which is stored in a temporary char*) into the tree. instead of sending the string of words for example "joe" the tree somehow manages to store "" inside the tree.
I cannot understand why this is happening, if anyone has some type of advise, please let me know.
Here is my code:
--------------------------------------------------------
char myString[MAX];
char* pMyString;
TreeEntry tempEntry;
TreeNode *root, *temp;
WordType sword;
char ans;
FILE *iFile;
CreateTree(&root);
if (TreeEmpty(root))
puts("Tree created and empty");
iFile=fopen("Easycase1.txt", "r");
if(iFile==NULL)
printf("ERROR Opening Input File\n");
start=clock();
while (!feof(iFile))
{ pMyString = myString;
*pMyString = toupper(fgetc (iFile));
while(*pMyString != '\n' && !feof(iFile) && *pMyString != ' ' && *pMyString != '.' && *pMyString != ',' && *pMyString != ';' && *pMyString != '"' )
{ pMyString++;
*pMyString = toupper(fgetc(iFile));
}
*pMyString = '\0';
if (isalpha(myString[0]))
{ printf("String = %s\n", myString);
tempEntry.word=myString;
tempEntry.count=1;
printf(" Word = %s\n", tempEntry.word);
root=InsertTree(root,MakeTreeNode(tempEntry));
}
}
fclose(iFile);
printf("\n\n");
Inorder(root,Print);
end=clock();
---------------------------------------------------------
I know that the tree works because if I create a string of strings and initialize them the following way: {"blue","green","yellow"} then they get inputed into the tree just fine.
also I know that my input works because I print out the words from the file and they are stored into the string and the TreeEntries just fine.
Thank You in advance for your help
All of my libraries have been tested and there seems to be no problem and I read each one of the words one by one from the file with no problem.
The problem arises when I try to send the word from the file(which is stored in a temporary char*) into the tree. instead of sending the string of words for example "joe" the tree somehow manages to store "" inside the tree.
I cannot understand why this is happening, if anyone has some type of advise, please let me know.
Here is my code:
--------------------------------------------------------
char myString[MAX];
char* pMyString;
TreeEntry tempEntry;
TreeNode *root, *temp;
WordType sword;
char ans;
FILE *iFile;
CreateTree(&root);
if (TreeEmpty(root))
puts("Tree created and empty");
iFile=fopen("Easycase1.txt", "r");
if(iFile==NULL)
printf("ERROR Opening Input File\n");
start=clock();
while (!feof(iFile))
{ pMyString = myString;
*pMyString = toupper(fgetc (iFile));
while(*pMyString != '\n' && !feof(iFile) && *pMyString != ' ' && *pMyString != '.' && *pMyString != ',' && *pMyString != ';' && *pMyString != '"' )
{ pMyString++;
*pMyString = toupper(fgetc(iFile));
}
*pMyString = '\0';
if (isalpha(myString[0]))
{ printf("String = %s\n", myString);
tempEntry.word=myString;
tempEntry.count=1;
printf(" Word = %s\n", tempEntry.word);
root=InsertTree(root,MakeTreeNode(tempEntry));
}
}
fclose(iFile);
printf("\n\n");
Inorder(root,Print);
end=clock();
---------------------------------------------------------
I know that the tree works because if I create a string of strings and initialize them the following way: {"blue","green","yellow"} then they get inputed into the tree just fine.
also I know that my input works because I print out the words from the file and they are stored into the string and the TreeEntries just fine.
Thank You in advance for your help