Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6729435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:17:23+00:00 2026-05-26T10:17:23+00:00

This project is a basic ATM program. I’m using a file to store all

  • 0

This project is a basic ATM program. I’m using a file to store all the account details. So, every time I run the .exe file, It will read the data from the file and insert it into an AVL tree. And when I close the program, all the data in the AVL nodes will be inserted back into the file.

Data is stored in the file in this order (Each separated by a newline char) ID, Password, Name, Add, City, Pin, Balance.
Sample file —

12
4576 
Vert 
No_999,GoLane 
Dallas 
89777 
50000 
16 
2342 
Nerd 
No_888,FoLane 
Chicago 
89999 
30000

The problem is I cannot write back data into the file. Any suggestions please?
P.S. Please excuse my inline class methods please…
Program–

#include<iostream>
#include<conio.h>
#include<string.h>
#include<fstream>
using namespace std;

fstream file("one2.txt",ios::in|ios::out);//Opening the file 'one2.txt' in global scope

//AVL tree code starts here

class avl
{
  struct node //The structure node which is going to hold the data sets in the tree
  {
     int id,pwd;
     char name[15],add[30],city[10];
     int pn;
     double bal;

     node *left, *right;
     int height;
     //node constructors
     node(int i,int p,char nam[15], char a[30], char c[10],int pin,double b, node * l,node * r,int h)
     {
              id=i;
              pwd=p;
              strcpy(name,nam);
              strcpy(add,a);
              strcpy(city,c);
              pn=pin;
              bal=b;
              left=l;
              right=r;
              height=h;
     }
     node()
     {
           left=right=NULL;
           id=pwd=pn=0;
           bal=0;
           height=-1;
     }
 };
 node *root;
 node *nullnode;

 int Height(node *t)const //Func to return the height of a node
 {
  return((t==NULL)? -1:t->height);
  }

 int max(int a,int b)
 {
  return(a>b)?a:b;
  }

  //Beginning of Insert() -- To create and insert data into the nodes 
  void insert(const int &x,int p, char nam[15], char a[30], char c[10],int pin,double b, node *&t)
  {
    if(t==NULL)
        t = new node(x,p,nam,a,c,pin,b,NULL,NULL,-1);
    else if(x<t->id)
    {
      insert(x,p,nam,a,c,pin,b,t->left);
      if(Height(t->left) - Height(t->right)==2)
      {
                     if(x<t->left->id)
                            single_rotate_with_left(t);
                     else
                         double_rotate_with_left(t);
      }
     }

     else if(x>t->id)
     {
       insert(x,p,nam,a,c,pin,b,t->right);
       if(Height(t->right)-Height(t->left)==2)
       {
              if(x>t->right->id)
                   single_rotate_with_right(t);
              else
                  double_rotate_with_right(t);
       }
     }
    else
       t->height=max(Height(t->left),Height(t->right)+1);
 }
 //End of insert()

 //Func to print the node data. Just a sample to check if all the data
 // were inserted into the tree
 //Inorder traversal 
 void print(node *&t)
 {
   if(t!=NULL)
   {
       print(t->left);
       cout<<endl;
       cout<<"ID "<<t->id<<" Name "<<t->name;
       cout<<endl<<t->pwd<<endl<<t->add<<"\n"<<t->city;
       cout<<"-"<<t->pn<<endl<<t->bal<<endl;
       print(t->right);
   }
 }

  //Think there's gonna be no problem with the rotation and other AVL tree func codes.
  //Beginning of AVL rotations 
  void single_rotate_with_left(node *&k2)
  {
     node *k1=k2->left;
     k2->left=k1->right;
     k1->right=k2;
     k2->height=max(Height(k2->right),Height(k2->left))+1;
     k1->height=max(Height(k1->left),(k2->height))+1;
     k1=k2;
  }
  void single_rotate_with_right(node *&k2)
  {
   node *k1=k2->right;
   k2->right=k1->left;
   k1->left=k2;
   k2->height=max(Height(k2->left),Height(k2->right))+1;
   k1->height=max(Height(k1->right),(k2->height))+1;
   k1=k2;
  }
   void double_rotate_with_left(node *&a)
  {
    single_rotate_with_right(a->left);
   single_rotate_with_left(a);
  }
 void double_rotate_with_right(node *&a)
 {
   single_rotate_with_left(a->right);
   single_rotate_with_right(a);
 }

 //End of AVL rotations

 //Function to return the node. The 'id' variable to be searched is passed as a param 
 node*& search(int x,node *&t)
 {

      if(t->id>x)
           return search(x,t->left);
      else if(t->id<x)
           return search(x,t->right);
      else if(t->id==x)
          {
              return t;
          }
      else
                  return nullnode;
 }
 //End of search. I'm using this in the loadnode() function.

 //This is where I try to write data back into the file.
 void update1(node *&t,int x) // x is the control variable
 {
   if(x==1)
   //This block will be executed only once when the function is called for the
   //first time. Used to seek to the beginning of the file
   {
          file.seekg(0,ios::beg);
          x++;
   }
   if(t!=NULL)// Inorder traversal in the tree
   {

             update1(t->left,x);

             //writing the data in the same order as it was stored. 
             file<<t->id<<endl;
             file<<t->pwd<<endl;
             file<<t->name<<endl;
             file<<t->add<<endl;
             file<<t->city<<endl;
             file<<t->pn<<endl;
             file<<t->bal<<endl;

             update1(t->right,x);
   }              
 }

 public:
     //Avl Constructor - This one is the one which is actually used.
     avl(int x,int p,char nam[15], char a[30], char c[10],int pin,double b)
     {
          root= new node(x,p,nam,a,c,pin,b,NULL,NULL,-1);
          nullnode=new node;
     }
     avl()
     {
          root->left=root->right=NULL;
          root->height=-1;
     }

     //Call to the private insert function
     void insert1(const int &x,int p,char nam[15], char a[30], char c[10],int pin,double b)
     {
          insert(x,p,nam,a,c,pin,b,root);
     }
     //Call to the private print() function
     void display()
     {
          cout<<endl;
          print(root);
     }



     //Function to write a new value for 'bal' variable to a node.
     //I'm actually using this to update a node anconfirm whether the value of the updated node 
     //is reflected back at the node 
     void loadnode(int x)
     {
          node *&t=search(x,root);
          cout<<"\nLoaded node...\n";
          cout<<t->id;
          cout<<" "<<t->name;
          t->bal=40000;
          cout<<"\nUpdated Bal.."<<t->bal;
     }



     void update()
     {
          //file.seekp(0);
          update1(root,1);
     }

};//End of AVL Class



main()
{ 

 cout<<"The output..\n";
 int i, p, pn;
 char n[15],a[30],c[10];
 double b;
 int prev_id=0;




 file>>i>>p>>n>>a>>c>>pn>>b;
 prev_id=i;
 avl list(i,p,n,a,c,pn,b);
 while(file)
  {

        file>>i>>p>>n>>a>>c>>pn>>b;
        if(prev_id!=i)
        // I'm using this because i got a weird scenario in which the last record was repeated twice.
        {

        list.insert1(i,p,n,a,c,pn,b);
        }
        prev_id=i;
  }



  cout<<endl<<"The elements in AVL tree are...\n\n";
  list.display();
  list.loadnode(12);//12 is the id i used for one of my records.
  //Calling to write back the data into the file.
  list.update();

  file.close();
  getch();
  return 0;
}

//End of program
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:17:23+00:00Added an answer on May 26, 2026 at 10:17 am

    If file.good() returned false, some previous operation on the file failed (maybe even a read operation) and raised one of the error flags of the file object. An ugly way to solve it is to use file.clear() which will clear the error flag and allow next actions to execute successfully. A better way to solve it will be to check after each operation if there’s an error (file.good() is false) and understand why this operation fails and fix it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the last part of this project, thanks to all who've helped with
I have created a really basic project (Make) like this: (ede-proj-project zrm :name zrm
So I'm running out of time on this project and I posted here in
Background : I am currently using custom controls within my C# project (basic controls
I have a flex library project, assume this project as basic utility which will
I'm creating my first project with ASP. This project is just making a basic
I've been working on this project for about a year now. It's a basic
This project is the probable first step in migrating a large CMS from Classic
This project started as a development platform because i wanted to be able to
On this project I am working on right now, one of the newest feature

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.