C problem How to write my Binary Search Tree into an output

C++ problem

How to write my Binary Search Tree into an output file. I have a completed BST.h file and main file reading a txt file into a BST and now I

want to write it into and output file. My professor said this:

A Word About Printing

Note that you will need to alter the signatures of preOrderPrint, inOrderPrint and postOrderPrint to take in information about a file.

Part of my BST.h file:

private:
   struct Node {
       bstitem data;
       Node* left;
       Node* right;

       Node(bstitem newdata) :
               data(newdata), left(NULL), right(NULL) {
       }
   };

   typedef struct Node* NodePtr;
   NodePtr root;

...

...

void inOrderPrintHelper(NodePtr root);

   void preOrderPrintHelper(NodePtr root);

   void postOrderPrintHelper(NodePtr root);

public:

......

void inOrderPrint();
   void preOrderPrint();
   void postOrderPrint();

My main file:

int main() {

   int value;
   char c;
   ifstream fin;
   fin.open(\"infile.txt\");

   if (fin.fail()) {
       cout << \"Input failed.\ \";
       exit(-1);
   }

   ofstream fout;
   fout.open(\"outfile.txt\");
   if (fout.fail()) {
       cout << \"Output file failed to open.\ \";
       exit(-1);
   }

   while (!fin.eof()) {
.......
       BST.preOrderPrint();
       BST.inOrderPrint();
.........

.........
       BST.postOrderPrint();
..........
}

\".....\" are some codes in between not important. I just want to know what I need to change to instead of printing onto the console now I\'m writing

it onto a file.

I tried to change my signature in my BST.h file like this but still error:

void inOrderPrint(fstream &S);

Solution

Signature is wrong u have to give,, ofstream for output stream instead of fstream

Just need to add below line to print value to file in the function you need to write to file as below .

S<<root->data<<endl;

C++ problem How to write my Binary Search Tree into an output file. I have a completed BST.h file and main file reading a txt file into a BST and now I want to
C++ problem How to write my Binary Search Tree into an output file. I have a completed BST.h file and main file reading a txt file into a BST and now I want to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site