Question: can you please find the problem below programme . it can 't read xml.txt file xml.txt file : Jake Adam Reminder Keep Hydrated! C programme

can you please find the problem below programme . it can 't read xml.txt file

xml.txt file :

Jake

Adam

Reminder

Keep Hydrated!

C programme file :

#include

#include

#include

struct node

{

char tag[20];

struct node *child;

};

struct node *root=NULL;

void open(char tag[20] )

{

struct node *t,*p;

t=(struct node*)malloc(sizeof(struct node));

strcpy(t->tag,tag);

t->child=NULL;

if(root==NULL)

{

root = t;

}

else

{

struct node *c;

c=root;

while(c)

{

p = c;

c = c->child;

}

p->child = t;

}

}

void close(char tag[20])

{

if(root==NULL)

{

printf("Error! Starting of %s tag not found. ",tag);

exit(0);

}

else if(strcmp(root->tag,tag)==0)

{

if(root->child==NULL)

{

root=NULL;

free(root);

}

else

{

printf("Error! Please close the %s tag.",root->child->tag);

exit(0);

}

}

else

{

struct node *t,*p;

t = root;

p=t;

while(t)

{

if(strcmp(t->tag,tag)==0)

{

break;

}

else

{

p=t;

t = t->child;

}

}

if(t==NULL)

{

printf("Error! Starting of %s tag not found. ",tag);

exit(0);

}

else if(t->child != NULL)

{

printf("Error! Please close the %s tag. ",t->child->tag);

exit(0);

}

else

{

p->child=NULL;

free(t);

}

}

}

void display()

{

struct node *t;

t = root;

while(t)

{

printf("%s ",t->tag);

t = t->child;

}

}

int main()

{

FILE *fp;

fp = fopen("C:\\CompreFile\\xml.txt","r");

char temp[100];

while( fscanf(fp, "%s",temp) != EOF )

{

//Condition for an opening tag

if(temp[0]=='<' && temp[1]!='/')

{

printf("%s ",temp);

open(temp);

}

// Condition for a closing tag

else if(temp[0]=='<' && temp[1]=='/')

{

char tag[20];

int j=0;

for(int i=0;i<10;i++)

{

if(i==1)

{

continue;

}

else

{

tag[j] = temp[i];

j++;

}

}

close(tag);

}

}

if(root==NULL)

{

printf("All tags closed successfully! ");

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!