I have tried thiss:
Code:
fgets(str,16,in2);
printf ("%s",str);
int i=0;
while(str[i]!='\0')
{
while(str[i]!=' ')
{
printf (" This does not have /0 in it = %c \n",str[i]);
i++;
}
}
but it only gets the characters. I want the word that the characters make up. When i can get the full string, I want to put it into another function to find it in the text file, when it finds it,
Currently, I am making a function so when it finds the word, the word (str) would point to the word next to it but using two separate pointers. Next(n) and Previous (p).
So
"Hi" would point to "Good" which will then point to "Day".
Thus Far, I have,
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
typedef struct node{
char data[100];
node *p,*n,*left,*right;
}Node, *NodePtr;
NodePtr MakeNode(char str[])
{
NodePtr p=(NodePtr) malloc (sizeof(Node));
strcpy(p->data,str);
p->left=NULL;
p->n=NULL;
p->p=NULL;
p->right=NULL;
return p;
}
NodePtr find(NodePtr root, char str[])
{
NodePtr curr=root ,tnp= MakeNode(str);
if (root== NULL) return tnp;
int cmp=strcmp(str,curr->data);
while (cmp!=0)
{
if (cmp<0)
{
if (curr->left==NULL)
{
curr->left=tnp;
return tnp;
}
curr=curr->left;
}
else
{
if (curr->right==NULL)
{
curr->right=tnp;
return tnp;
}
curr=curr->right;
}
}
return curr;
}
NodePtr adjust(FILE *in, NodePtr root)
{
NodePtr curr;
char str[100];
NodePtr tnp;
while (!feof(in))
{
fscanf(in,"%s",str);
tnp=find(root,str);
if (tnp!=NULL)
{
if (root->n==NULL)
tnp->n=adjust(in,curr->p);
}
}
return 0;
}
char main ()
{
NodePtr root=NULL, tnp;
char str[16];
FILE * in=fopen("C:\\Test\\input.txt","r");
FILE* out=fopen("C:\\Test\\input.txt","a");
FILE *in2=fopen("C:\\Test\\input.txt","r");
/* printf( " Enter The Words You Would Like To Add\n");
scanf ("%s",&str);
while (strcmp(str,"0")!=0)
{
fprintf(out," %s",str);
scanf("%s",&str);
}
*/
while (!feof(in))
{
fscanf(in,"%s",str);
NodePtr tnp=find(root,str);
if (root==NULL) root=tnp;
}
adjust(in2,root);
/* printf( " Enter the Word you would like to find \n");
scanf("%s",&str);
printf (" We found it here %p ",find(root,str));
printf ("\n In Order = ");
InOrder(root);
printf ("\nPost Order = ");
PostOrder(root);
printf ("\nPre Order = ");
PreOrder(root);
fgets(str,16,in2);
printf ("%s",str);
int i=0;
while(str[i]!='\0')
{
while(str[i]!=' ')
{
printf (" This does not have /0 in it = %c \n",str[i]);
i++;
}
}*/
fclose(out);
fclose(in);
getch();
}