📄 cformrestoredata.cpp
字号:
#include "link.h"
/************************************************************************
文件还原操作函数
************************************************************************/
void CFormRestoreData::load()
{
RestoreForm();
do{
if(RestoreData()==-1)
return;
if(RestoreData())
break;
}while(1);
}
/************************************************************************
文件数据还原框架
************************************************************************/
void CFormRestoreData::RestoreForm()
{
CFormBase::load();
string title("Restore data");
int len=title.length();
GotoXY((80-len)/2,3);
cout<<title<<endl;
GotoXY(2,21);
cout<<"The filename cannot include , . * | > < ; ?"<<ends;
GotoXY(18,12);
cout<<"Input filename:"<<ends;
}
/************************************************************************
文件名输入
************************************************************************/
string CFormRestoreData::InputFilename()
{
string tempfilename;
do
{
int x,y;
WhereXY(&x,&y);
GotoXY(x,y);
CMessageBox::Clear(x,40);
GetString(tempfilename,40);
int len=tempfilename.length();
if(len==0)
{
CMessageBox::ShowMessage("Filename cannot be null,please input again !");
continue;
}
if(tempfilename[len-1]==27)
break;
if(CMyString::CheckFileName(tempfilename))
{
if(CMyString::CheckDiskName(tempfilename))
{
len=tempfilename.length();
if(tempfilename[len-1]=='\\')
tempfilename[len-1]='\0';
break;
}
}
else
{
continue;
}
}while(1);
return tempfilename;
}
/************************************************************************
还原数据
************************************************************************/
int CFormRestoreData::RestoreData()
{
char str[100];
int x,y;
GotoXY(34,12);
WhereXY(&x,&y);
GotoXY(x,y);
string s=InputFilename();
int len=s.length();
if(s[len-1]==27)
return -1;//退出,返回主菜单
filename=(char *)s.c_str();
sprintf(str,"The file [%s] doesnot exit !Please have a check.",filename);
if(access(filename,0)==-1)
{
CMessageBox::ShowMessage(str);
return 0;
}
sprintf(str,"Are you sure to restore data from [%s] ?[Y/N]",filename);
CMessageBox::ShowMessage(str);
char choice;
WhereXY(&x,&y);
do
{
GotoXY(x,y);
choice=getch();
if (isalpha(choice))
{
cout<<choice;
if (choice=='Y'||choice=='y')
{
//从文件filename中读取数据覆盖默认文件
if(GetData())
{
CMessageBox::ShowMessage("Restore data success !");
CDateTime::DelayTime(2);
return 1;
}
else
return 0;
}
else if(choice=='N'||choice=='n')
{
//直接返回主菜单
return 0;
}
else
{
//提示,重新输入选项
CMessageBox::ShowMessage("Input must be Y or N !");
continue;
}
}
else
{
CMessageBox::ShowMessage("You must input letter !");
continue;
}
} while (1);
return 1;
}
/*
string CFormRestoreData::FindRestoreFile()
{
//确认路径
//查询文件名的后缀为.bak的文件,并将文件名保存在容器中,未做
int count=0;
int num=1;
string tempfilename;
GotoXY(15,15);
cout<<"Input filename:"<<ends;
GetString(tempfilename,50);
filename=(char *)tempfilename.c_str();
return tempfilename;
}
string CFormRestoreData::InputNum()
{
string snum;
int x,y,len;
WhereXY(&x,&y);
do
{
CMessageBox::Clear(x,3);
GetString(snum,3);
CMyString::RemoveLeftBlank(snum);
CMyString::RemoveRightBlank(snum);
len=snum.length();
if(snum[len-1]==27)
return snum;
for(int i=0;i<len;i++)
{
if(!isdigit(snum[i]))
break;
}
if(i<len)
continue;
else
break;
}while(1);
GotoXY(x,y);
CMessageBox::Clear(x,3);
return snum;
}*/
/************************************************************************
读取数据函数
************************************************************************/
bool CFormRestoreData::GetData()
{
CAccManager& accman=CAccManager::GetInstance();
CConManager& conman=CConManager::GetInstance();
CAppMoneyManager& appman=CAppMoneyManager::GetInstance();
vector<CAccount>& vecacc=accman.GetVecAccount();
vector<CConsume>& veccon=conman.GetConsumeList();
vector<CAppMoney>& vecapp=appman.GetVecMoneyList();
//清空内存数据
vecacc.clear();
veccon.clear();
vecapp.clear();
//
fstream file;
file.open(filename,ios::in|ios::binary);
if(access(filename,0)==-1)
return false;
if (!file)
{
cerr<<"File Open Fail!!"<<endl;
return 0;
}
//账户信息还原
int i,count,len,_id;
float _balance;
char tempstr[200];
CAccount tempacc;
file.read((char*)&count,sizeof(count));
for (i=0;i<count;i++)
{
//读取学号
file.read((char *)&_id,sizeof(_id));
tempacc.SetID(_id);
//读取性别
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetSex(tempstr);
//读取姓名
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetName(tempstr);
//读取生日
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetBirth(tempstr);
//读取电话
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetMobile(tempstr);
//读取余额
file.read((char *)&_balance,sizeof(_balance));
tempacc.SetBalance(_balance);
//读取状态
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempacc.SetStat(tempstr);
//临时账户添加到向量容器
vecacc.push_back(tempacc);
}
//消费记录还原
float _money;
string _idlist;
string _date;
int _mancount;
CConsume tempcon;
file.read((char*)&count,sizeof(count));
for (i=0;i<count;i++)
{
//读取编号
file.read((char *)&_id,sizeof(_id));
tempcon.SetID(_id);
//读取消费人数
file.read((char *)&_mancount,sizeof(_mancount));
tempcon.SetManCount(_mancount);
//读取消费编号名单表
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempcon.SetIDList(tempstr);
//读取日期
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempcon.SetDate(tempstr);
//读取消费值额
file.read((char *)&_money,sizeof(_money));
tempcon.SetMoney(_money);
//读取备注
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempcon.SetComment(tempstr);
//临时账户添加到向量容器
veccon.push_back(tempcon);
}
//充值记录还原
string _name;
CAppMoney tempapp;
file.read((char*)&count,sizeof(count));
for (i=0;i<count;i++)
{
//读取编号
file.read((char *)&_id,sizeof(_id));
tempapp.SetID(_id);
//读取姓名
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempapp.SetName(tempstr);
//读取日期
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempapp.SetDate(tempstr);
//读取充值额
file.read((char *)&_money,sizeof(_money));
tempapp.SetMoney(_money);
//读取备注
file.read((char *)&len,sizeof(len));
file.read(tempstr,len);
tempstr[len]=0;
tempapp.SetComment(tempstr);
//临时账户添加到向量容器
vecapp.push_back(tempapp);
}
//保存到默认文件
accman.SaveData();
conman.SaveConsumeData();
appman.SaveAppMoneyData();
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -