C++ 中的文件 I/O - 写回数据时遇到问题?
本项目是一个基本的ATM程序。我正在使用一个文件来存储所有帐户详细信息。因此,每次运行 .exe 文件时,它都会从文件中读取数据并将其插入到 AVL 树中。当我关闭程序时,AVL节点中的所有数据都将被插入回文件中。
数据按此顺序存储在文件中(每个内容均以换行符分隔)ID、密码、姓名、添加、城市、密码、余额。 示例文件——
12
4576
Vert
No_999,GoLane
Dallas
89777
50000
16
2342
Nerd
No_888,FoLane
Chicago
89999
30000
问题是我无法将数据写回到文件中。请问有什么建议吗? PS 请原谅我的内联类方法... 程序 -
#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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 file.good() 返回 false,则先前对文件的某些操作失败(甚至可能是读取操作)并引发文件对象的错误标志之一。解决这个问题的一个丑陋的方法是使用 file.clear() ,它将清除错误标志并允许下一步操作成功执行。解决此问题的更好方法是在每次操作后检查是否有错误(file.good() 为 false)并了解此操作失败的原因并修复它。
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.
调用
seekp()
将写指针移动到流的开头 (fstream
)。seekg()
移动获取指针 - 写入时不会有帮助...call
seekp()
to move the write pointer to the begining of the stream (fstream
).seekg()
moves the get pointer - not going to help when writing...