Hi, how can I convert the data saved and restore it from a .txt file instead by a binary file from p

Hi, how can I convert the data saved and restore it from a .txt file instead by a binary file from pointers? I need it as .txt file because it shows strange characters in the file when I try to open normally with a text editor.

Code:

#include

using std::cout;

using std::cin;

using std::endl;

using std::ios;

#include

using std::fstream;

#include

//struct definition

struct animal {

  char text[100];

  animal* yes;

  animal* no;

};

//function templates

void deallocateTree(animal*);

void saveTree(animal*, fstream&);

void restoreTree(animal*, fstream&);

int main() {

  animal* root = new animal;

  strcpy(root->text, “Elephant”);

  root->yes = 0;

  root->no = 0;

  // restore tree from a disk file

  fstream fin;

  fin.open(“Animal.dat”, ios::in|ios::binary);

  restoreTree(root, fin); // create an empty root node before calling this

  fin.close();

  while(true) {

    char buf[100];

    cout

    cin >> buf;

    if (toupper(buf[0]) == 'N')

      break;

    animal* p = root;

    while(p) {

      if(p->yes == 0) {

        bool correctGuess;

        while (true) {

          cout << p->text

          cin >> buf;

          if (toupper(buf[0]) == &#39;Y&#39;) {

            cout

            correctGuess = true;

            break;

          }

          else if (toupper(buf[0]) == &#39;N&#39;) {

            correctGuess = false;

            break;

          }

          else if (toupper(buf[0]) == &#39;E&#39;) {

            cin.ignore(1000,10);

            cout

            cin.getline(p->text,100);

          }

        }

        if (correctGuess == true)

          break;

        char a[100], q[100], yN[100];

        cout

        cin >> a;

        cin.ignore(1000,10);

        cout << "What yes/no question differentiates " << p->text

        cin.getline(q, 100);

        cout

        cin >> yN;

        cin.ignore(1000,10);

        animal* y = new animal;

        animal* n = new animal;

        if (toupper(yN[0]) == &#39;Y&#39;) {

          strcpy(y->text, a);

          strcpy(n->text, p->text);

        }

        else if (toupper(yN[0]) == &#39;N&#39;) {

          strcpy(n->text, a);

          strcpy(y->text, p->text);

        }

        strcpy(p->text, q);

        y->yes = 0;

        n->yes = 0;

        y->no = 0;

        n->no = 0;

        p->yes = y;

        p->no = n;

        break;

      }

      else {

        while (true) {

          char yN[100];

          cout << p->text

          cin >> yN;

          cin.ignore(1000,10);

          if (toupper(yN[0]) == &#39;Y&#39;) {

            p = p->yes;

            break;

          }

          else if (toupper(yN[0]) == &#39;N&#39;) {

            p = p->no;

            break;

          }

          else if (toupper(yN[0]) == &#39;E&#39;) {

            cout

            cin.getline(p->text,100);

          }

          else if (toupper(yN[0]) == &#39;D&#39;) {

            deallocateTree(p->yes);

            deallocateTree(p->no);

            strcpy(p->text, “Elephant”);

            p->yes = 0;

            p->no = 0;

            break;

          }

        }

      }

    }

  }

  // save tree to a disk file

  fstream fout;

  fout.open(“Animal.dat”, ios::out|ios::binary);

  saveTree(root, fout);

  fout.close();

  deallocateTree(root);

}

void deallocateTree(animal* a) {

  if (!a) return;

  deallocateTree(a->yes);

  deallocateTree(a->no);

  delete a;

}

// function definitions — save and restore

void saveTree(animal* a, fstream& out) { // saves tree to disk file

  if (a) {

    out.write((char*)a, sizeof(animal));

    saveTree(a->yes, out);

    saveTree(a->no, out);

  }

}

void restoreTree(animal* a, fstream& in) { // loads tree from disk file

  in.read((char*)a, sizeof(animal));

  if(a->yes) {

    restoreTree(a->yes = new animal, in);

    restoreTree(a->no = new animal, in);

  }

}

 

Looking for a Similar Assignment? Hire our Top Uk Tutors while you enjoy your free time! All papers are written from scratch and are 100% Original. Try us today! Active Discount Code FREE15