hello everyone my aim is to count different words from text file and write them to output file with the number of occurences.but my code didn't work,I don't know the mistake but I think my read and write functions cause the problem.any help or suggestions?
#include<iostream>
#include<string>
#include<fstream>
#define FILEIN "acar.txt"
#define FILEOUT "kapa.txt"
using namespace std;
struct Data // Data include keyword and number of occurrences
{
string key;
int count;
};
struct Node //A Node inclues Data, left and right
{
Data data;
Node* left;
Node* right;
};
void ReadFile(Node* t,char *filein);
void WriteFile(Node* p,char* fileout);
Node* InsertData(Node* t,Data DataInsert);
Node* AddNode(Node *t,Node* v );
bool Compare(Node* n, string s);
void ReadFile(Node* t,char *filein)
{
ifstream fin;
fin.open(filein);
if(!fin.good())
{
cout<<"Error!File wordcount.in is not exits!"<<endl;
cout<<"Please check again !"<<endl;
}
else
{
t=NULL;
Data getdata;
string word;
while(!fin.eof())
{
getdata.count=1;
fin>>word;
getdata.key=word;
t=InsertData(t,getdata);
}
}
fin.close();
}
//Print all values of nodes inoder traversal
void WriteFile(Node* p,char* fileout)
{
ofstream fout(fileout);
if(p!=NULL)
{
WriteFile(p->left,fileout);
fout<<p->data.key<<"\t";
fout<<p->data.count<<endl;
WriteFile(p->right,fileout);
}
fout.close();
}
Node* InsertData(Node* T,Data DataInsert)
{
Node* temp=new Node;
temp->data=DataInsert;
temp->left=NULL;
temp->right=NULL;
return AddNode(T,temp);
}
Node* AddNode(Node *v1,Node* v2 )
{
if(v1==NULL) return v2;
else
{
if(v1->data.key==v2->data.key)
{
v1->data.count++;
return v1;
}
else
if(v1->data.key <v2->data.key)
{
v1->left=AddNode(v1->left,v2);
return v1;
}
else
{
v1->right=AddNode(v1->right,v2);
return v1;
}
}
}
//main function
int main()
{
Node* Tree=new Node;
ReadFile(Tree,FILEIN);
WriteFile(Tree,FILEOUT);
}