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);
	}

In your struct Node,

struct Node
{
Data data;
Node* left;
Node* right;
 
};

What do the variables left and right mean?

Can you explain the logic that you want to implement?

Thanks

it is the classical binary tree logic.every node has two child:right and left...and I have to do this project with using binary search tree

Kyros

Your code to build the data structure is good, and works fine. the problem is with the WriteFile code.
The first problem is that it doesn't start at the root of the data structure. FIx by returning root node from ReadFile and pass into WriteFIle.
The second problem is a bit subtler. Because you are calling WriteFile recursively, you are dropping out of WriteFile (and closing the file) every time you reach a data structure 'leaf'. Actually you close the file once per unique word (before you finish processing the whole data structure).

Let me know how you get on?

Duncan

First off, your formatting makes your code difficult to read. See this for assistance.

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?

We don't know the mistake either, since you didn't bother to tell us what the problem is. All you said was the equivalent of "My car won't move". Be explicit in your descriptions of the problem.

If you suspect the read function, display what is read. cout is the easiest debug tool available.

Also, I don't know about your native language, but in English, there is a space after the period (.) at the end of a sentence. English isn't a web address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.