本人想做一个个人账户信息管理系统,用的是文件的读写去实现功能,整体思路就是我自己想用两个.dat文件一个存用户,另一个存用户信息,第一个文件来实现登录注册(模拟登录注册),第二个文件里面每一个信息里面都有用户名字,方便后面查找每个用户对应信息,现在就是我能实现写入到文件里面,还有读到程序里面进行输出,但是每次都会报一个异常(附图)..自己看了好久都不知道到底是哪里出了问题,希望各位大佬能指点一下迷津。。
--------------------------------------------------------------------------------------------------------------------------------------------
代码:
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<Exception>
using namespace std;
struct User//用户结构体
{
unsigned int flag;//权值
string name;//用户名
string password;//用户密码
};
struct User2
{
string name2;
string Aname;//用户个人账户名称
string Apassword;
};
class UserAccount
{
private:
// fstream file;//建立用户文件;
// fstream file2;//建立用户信息薄问文件
string a1;//用作后面获取到登录用户的用户名
ifstream infile;//用作写
ofstream outfile;//用作读
ifstream infile2;//用作写(表2)
ofstream outfile2;//用作读(表2)
void Login();
void Register();
void AddDate();
void Show();
void UpDate();
public:
UserAccount();
// virtual~UserAccount() { file.close(); }
void Run();//实现登录注册界面
void Run2();
};
//------------------------------------------------------------------------
UserAccount::UserAccount()//创造用户信息文件
{
ifstream iFile("UserAccount.dat");
if (iFile.fail())
{
ofstream oFile("UserAccount.dat");
if (oFile.fail()) throw("打开文件失败.");
oFile.close();
}
else
{
iFile.close();
}
// file.open("UserAccount.dat", ios::in | ios::out | ios::binary | ios::app);
ifstream infile("UserAccount.dat", ios::in | ios::binary | ios::app);
ofstream outfile("UserAccount.dat", ios::out | ios::binary | ios::app);
//---------------------------------------------------------------------------------
ifstream iFile2("UserAccount2.dat");//表2的实例化
if (iFile2.fail())
{
ofstream oFile2("UserAccount2.dat");
if (oFile2.fail()) throw("打开文件失败.");
oFile2.close();
}
else
{
iFile2.close();
}
// file2.open("UserAccount2.dat", ios::in | ios::out | ios::binary | ios::app);
ifstream infile2("UserAccount2.dat", ios::in | ios::binary | ios::app);
ofstream outfile2("UserAccount2.dat", ios::out | ios::binary | ios::app);
}
//------------------------------------------------------------------------------
void UserAccount::Login()//登录界面
{
User acc;
User acc1;
cout << "欢迎使用本系统!" << endl;
cout << "用户名:";
cin >> acc.name;
cout << "密码:";
cin >> acc.password;
int flag = 0;
infile.seekg(0);
infile.open("UserAccount.dat", ios::in | ios::binary | ios::app);
infile.read((char*)& acc1, sizeof(User));
while (!infile.eof())
{
if (acc.name == acc1.name && acc.password == acc1.password)
{
flag = 1;
break;
}
infile.read((char*)& acc1, sizeof(User));
}
if (flag)
{
if (acc1.flag == 10)//权值为10则是管理员;
{
a1= acc.name;//获取到当前登录的用户名;
cout << "欢迎您!"<<"尊敬的管理员" <<acc1.name<<"!"<<" "<<"您的权值为:"<<acc1.flag<< endl;
}
else if(acc1.flag<10)
{
a1= acc1.name;//获取到当前登录的用户名;
cout << "欢迎您!尊敬的用户"<<acc1.name << " " << "您的权值为:" << acc1.flag << endl;
}
infile.close();
infile.clear();
Run2();//实现登录后的界面
}
else
{
cout << "用户名或密码错误!请仔细检查后重新输入!" << endl;
infile.close();
infile.clear();
Login();
}
}
//----------------------------------------------------------------------------
void UserAccount::Register()//注册页面
{
User acc;
User acc1;
cout << "欢迎加入我们!" << endl;
cout << "输入用户名称:";
cin >> acc.name;//输入用户名称
cout << "输入用户密码:";
cin >> acc.password;//输入用户密码
if (acc.name == "张鑫益")
{
acc.flag = 10;//给到最高权值
}
else
{
acc.flag = rand() % 10;
}
infile.seekg(0);
int fla = 1;//默认没有注册
infile.open("UserAccount.dat", ios::binary | ios::app);
infile.read((char*)& acc1, sizeof(User));//进行查找是否有用户名重名
while (!infile.eof())
{
if (acc.name == acc1.name)
{
fla = 0;
break;
}
infile.read((char*)& acc1, sizeof(User));//继续查找
}
if (fla)
{
cout << "恭喜您!注册成功!" << endl;
infile.close();
infile.clear();
outfile.open("UserAccount.dat",ios::out |ios::binary | ios::app);
outfile.write((char*)& acc, sizeof(User));
outfile.close();
Run();
return ;
}
else
{
cout << "对不起,该用户名已被注册!请重新输入哦!" << endl;
infile.close();//重新输入的时候要对流进行刷新
infile.clear();
outfile.close();
Register();
return;
}
}
//--------------------------------------------------------------------------
void UserAccount::Run()
{
int select;
do
{
cout << "欢迎使用本系统!" << endl;
cout << "1.登录 2.注册" << endl;
cout << " 3.退出 " << endl;
cin >> select;
while (cin.get() != '\n');
switch (select)
{
case 1:
try
{
Login();
}
catch (exception e)
{
cout << "----";
}
break;
case 2:
try
{
Register();
}
catch (exception e)
{
cout << "----";
}
break;
}
} while (select != 3);
}
//--------------------------------------------------------------------------
void UserAccount::AddDate()
{
User2 aac;
aac.name2 = a1;
outfile2.open("UserAccount2.dat", ios::out | ios::binary | ios::app);
cout << "请输入需要记录的账户名称:";
cin >> aac.Aname;
cout << "请输入对应账户的账户密码:";
cin >> aac.Apassword;
outfile2.write((char*)& aac, sizeof(User2));
outfile2.close();
}
//---------------------------------------------------------------------------
void UserAccount::Show()
{
User2 aac;
infile.open("UserAccount2.dat", ios::in | ios::binary | ios::app);
infile.read((char*)& aac, sizeof(User2));
while (!infile.eof())
{
if (aac.name2 == a1)
{
cout << "账户名称:"<< aac.Aname << " " << "账户密码:"<< aac.Apassword << endl;
}
infile.read((char*)& aac, sizeof(User2));
}
infile.clear();
infile.close();
}
//---------------------------------------------------------------------------
void UserAccount::Run2()
{
int selet;
do
{
cout << "请选择功能:" << endl;
cout << "1.增加账户信息" << endl;
cout << "2.查看账户信息" << endl;
cout << "3.查询账户信息" << endl;
cin >> selet;
switch (selet)
{
case 1:
try
{
AddDate();}
catch (exception e)
{
cout << "----";
}
break;
case 2:
try
{
Show();
}
catch(exception e)
{
cout << "----";
}
break;
}
} while (selet != 5);
}
//-------------------------------------------------------------------------
int main()
{
try
{
UserAccount a;
a.Run();
}
catch (exception err)
{
cout<<"----";
}
system("pause");
return 0;
}