求助:文件输入输出
编译已通过,但是存取款写不进文件里,而且注册账号时也有问题,是打开文件的方式有问题吗?谢谢了~
#include"iostream"
using namespace std;
#include"fstream"
#include"string.h"
int numbers=0;
class Customer
{
public:
long account;
//char code[20]; //这样子读出来是乱码
long code;
char name[20];
double balance;
Customer(long a,long c,char* n,double b):account(a),balance(b),code(c){
strcpy(name,n);
}
Customer(){
}
void Deposit(double m)
{
balance+=m;
}
int Draw(double m)
{
if(m>balance) return 0;
else
balance-=m;
return 1;
}
void Display()
{
cout<<"账号:"<<account<<endl;
cout<<"余额:"<<balance<<endl;
}
};
int Validate_account(long a)
{
fstream in("Customer.dat",ios::in|ios::binary);
Customer* p=new Customer(000,000,"000",0);
while(in)
{
in.read((char*)p,sizeof(Customer));
if(a==p->account)
{
in.close();
return 0;
}
continue;
}
in.close();
return 1;
}
int Validate_code(long c,Customer* p)
{
if(c==p->code)
return 1;
return 0;
}
int Write(Customer* p)
{
cout<<"账号:";
cin>>p->account;
int t;
if(numbers==0) t=1;
else t=Validate_account(p->account);
if(t)
{
cout<<"姓名:";
cin>>p->name;
cout<<"密码:";
cin>>p->code;
cout<<"确认密码:";
//char* code1; //为什么改成了char 数组就不行了呢?密码都验证不了
long code1;
cin>>code1;
if(Validate_code(code1,p))
{
fstream out("Customer.dat",ios::app|ios::out|ios::binary); //为什么如果是Customer.dat读出来是乱码?
out.write((char*)p,sizeof(Customer));
out.close();
return 1;
}
else
{
cout<<"确认密码有误,请重新注册!"<<endl;
return 0;
}
}
else cout<<"该账号已存在,请重新注册!"<<endl;
return 0;
}
main()
{
while(1)
{
cout<<"1.注册"<<endl;
cout<<"2.登陆"<<endl;
cout<<"0.退出"<<endl;
int t1;
cin>>t1;
if(t1==1)
{
Customer* p=new Customer(000,000,"000",0);
if(Write(p))
{
numbers++;
cout<<"注册成功!"<<endl;
cout<<"************************"<<endl;
}
delete p;
}
if(t1==2)
{
long a;
cout<<"账号:";
cin>>a;
Customer* p1=new Customer(000,000,"000",0);
fstream io("Customer.dat",ios::in|ios::out|ios::app|ios::binary);
while(io)
{
io.read((char*)p1,sizeof(Customer));
if(p1->account==a)
break;
}
if(!io)
{
cout<<"该账号用户不存在!"<<endl;
}
else{
cout<<"密码:";
long c;
cin>>c;
if(Validate_code(c,p1))
{
while(1)
{
cout<<"1.存款"<<endl;
cout<<"2.取款"<<endl;
cout<<"3.查询余额"<<endl;
cout<<"0.回到注册登录菜单"<<endl;
int t2;
cin>>t2;
if(t2==1)
{
cout<<"请输入存款金额:";
double b;
cin>>b;
p1->Deposit(b);
io.seekp(-sizeof(Customer),ios::cur);
io.write((char*) p1,sizeof(Customer));
cout<<"交易成功!"<<endl;
cout<<"**********************"<<endl;
}
if(t2==2)
{
cout<<"请输入取款金额:";
io.seekg(-sizeof(Customer),ios::cur);
io.read((char*) p1,sizeof(Customer));
double b_temp;
cin>>b_temp;
if( p1->Draw(b_temp))
{
io.seekp(-sizeof(Customer),ios::cur);
io.write((char*) p1,sizeof(Customer));
// io.seekg(-sizeof(Customer),ios::cur);
//io.read((char*) p1,sizeof(Customer));
cout<<"交易成功!"<<endl;
cout<<"**********************"<<endl;
}
else
{
cout<<"余额不足!"<<endl;
cout<<"**********************"<<endl;
}
}
if(t2==3)
{
Customer* p2=new Customer(000,000,"000",0); //我是想检测一下文件里的内容是否改变了
io.seekp(-sizeof(Customer),ios::cur);
io.read((char*) p2,sizeof(Customer));
p2->Display();
delete p2;
// p1->Display();
cout<<"**********************"<<endl;
}
if(t2==0) break;
}
io.close();
}
else{cout<<"密码错误!"<<endl;}
}
}
if(t1==0) break;
}
}