C ++中的文件I / O - 写回数据有些麻烦?

时间:2011-10-20 16:29:27

标签: c++ file file-io

该项目是一个基本的ATM程序。我正在使用文件存储所有帐户详细信息。因此,每次运行.exe文件时,它都会从文件中读取数据并将其插入到AVL树中。当我关闭程序时,AVL节点中的所有数据都将插回到文件中。

数据按此顺序存储在文件中(每个数字用换行符分隔)ID,密码,名称,添加,城市,固定,平衡。 示例文件 -

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

问题是我无法将数据写回文件。有什么建议吗? 附:请原谅我的内联课程方法... 程序 -

#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

2 个答案:

答案 0 :(得分:0)

调用seekp()将写指针移动到流的开头(fstream)。 seekg()移动get指针 - 写入时不会有帮助......

答案 1 :(得分:0)

如果file.good()返回false,则对文件的某些先前操作失败(甚至可能是读取操作)并引发文件对象的错误标志之一。解决它的一种丑陋方法是使用file.clear()来清除错误标志并允许下一步操作成功执行。解决问题的更好方法是在每次操作后检查是否存在错误(file.good()为false)并理解为什么此操作失败并修复它。