📄 cformbackupdata.cpp
字号:
#include "link.h"
/************************************************************************
文件备份操作函数
************************************************************************/
void CFormBackupData::load()
{
do{
BackupForm();
int x,y;
WhereXY(&x,&y);
char ch;
do
{
ch=getch();
if (isdigit(ch))
{
if (ch<'3')
{
break;
}
else
{
CMessageBox::ShowMessage("Input must be in 0 to 2 !");
continue;
}
}
else
{
CMessageBox::ShowMessage("Input must be digit !");
continue;
}
} while (1);
switch(ch)
{
case '1'://当前目录下备份
if(SaveData(1))
{
CMessageBox::ShowMessage("Backup data success.");
CDateTime::DelayTime(1);
return;
}
else
continue;
case '2'://备份另存为
//输入路径确认后,工作目录转到该文件夹
if(SaveData(2))
{
CMessageBox::ShowMessage("Backup data success.");
CDateTime::DelayTime(1);
return;
}
else
continue;
case '0':return;//返回主菜单
}
}while(1);
}
/************************************************************************
文件备份菜单框架
************************************************************************/
void CFormBackupData::BackupForm()
{
CFormBase::load();
string title("Backup data");
int len=title.length();
GotoXY((80-len)/2,3);
cout<<title<<endl;
GotoXY(25,7);
string temp="1.Backup data to save at current folder.";
cout<<temp<<ends;
GotoXY(25,8);
temp="2.Backup data to save in other folder.";
cout<<temp<<ends;
GotoXY(25,9);
temp="0.Return to menu.";
cout<<temp<<ends;
temp="Make a choice [0-2]:";
GotoXY(25,11);
cout<<temp<<ends;
}
/************************************************************************
文件名输入
************************************************************************/
string CFormBackupData::InputFilename()
{
CDateTime datetime;
string s=datetime.GetDateTime();
s+=".bak";
filename=s;
return filename;
}
/************************************************************************
文件备份路径输入
************************************************************************/
string CFormBackupData::InputBackupPath()
{
string filepath;
do
{
GotoXY(25,7);
CMessageBox::Clear(25,40);
GotoXY(25,8);
CMessageBox::Clear(25,40);
GotoXY(25,9);
CMessageBox::Clear(25,40);
GotoXY(25,11);
CMessageBox::Clear(25,40);
GotoXY(2,21);
cout<<"The filename cannot include , . * | > < ; ?"<<ends;
GotoXY(30,12);
cout<<"Input the folder :"<<ends;
int x,y;
WhereXY(&x,&y);
GotoXY(x,y);
GetString(filepath,50);
int len=filepath.length();
if(len==0)
{
CMessageBox::ShowMessage("Filename cannot be null,please input again !");
continue;
}
if(filepath[len-1]==27)
return filepath;
if(CMyString::CheckFileName(filepath))
{
if(CMyString::CheckDiskName(filepath))
{
return filepath;
}
}
else
{
continue;
}
}while(1);
}
/************************************************************************
数据备份函数
************************************************************************/
bool CFormBackupData::SaveData(int k)
{
CAccManager& accman=CAccManager::GetInstance();
CConManager& conman=CConManager::GetInstance();
CAppMoneyManager& appman=CAppMoneyManager::GetInstance();
char tempname[50];
int i;
string filepath;
InputFilename();
if(k==2)
{
filepath=InputBackupPath();
int len=filepath.length();
if(filepath[len-1]==27)
return false;
CMyString::CheckFoundFiles(filepath);
filename=filepath+filename;
}
int len=filename.length();
strcpy(tempname,(char *)filename.c_str());
fstream file;
file.open(tempname,ios::out|ios::binary);
if (!file)
{
cerr<<"File Open or Create Fail!"<<endl;
return 0;
}
//账户信息备份
int count,id;
float balance;
vector<CAccount> vecacc=accman.GetVecAccount();
count=vecacc.size();
file.write((char *)&count,sizeof(count));
for (i=0;i<count;i++)
{
//保存id
id=vecacc[i].GetID();
file.write((char *)&id,sizeof(id));
//保存性别
len=vecacc[i].GetSex().length();
file.write((char *)&len,sizeof(len));
file.write(vecacc[i].GetSex().c_str(),len);
//保存姓名
len=vecacc[i].GetName().length();
file.write((char *)&len,sizeof(len));
file.write(vecacc[i].GetName().c_str(),len);
//保存生日
len=vecacc[i].GetBirth().length();
file.write((char *)&len,sizeof(len));
file.write(vecacc[i].GetBirth().c_str(),len);
//保存电话
len=vecacc[i].GetMobile().length();
file.write((char *)&len,sizeof(len));
file.write(vecacc[i].GetMobile().c_str(),len);
//保存余额
balance=vecacc[i].GetBalance();
file.write((char *)&balance,sizeof(balance));
//保存状态
len=vecacc[i].GetStat().length();
file.write((char *)&len,sizeof(len));
file.write(vecacc[i].GetStat().c_str(),len);
}
int _id;
string _idlist;
string _date;
float _money;
int _mancount;
string _comment;
//消费记录备份
vector<CConsume> veccon=conman.GetConsumeList();
count=veccon.size();
file.write((char *)&count,sizeof(count));
for (i=0;i<count;i++)
{
//保存id
_id=veccon[i].GetID();
file.write((char *)&_id,sizeof(_id));
//保存消费人数
_mancount=veccon[i].GetManCount();
file.write((char *)&_mancount,sizeof(_mancount));
//保存消费编号名单表
len=veccon[i].GetIDList().length();
file.write((char *)&len,sizeof(len));
_idlist=veccon[i].GetIDList();
file.write(_idlist.c_str(),len);
//保存日期
len=veccon[i].GetDate().length();
file.write((char *)&len,sizeof(len));
file.write(veccon[i].GetDate().c_str(),len);
//保存消费金额
_money=veccon[i].GetMoney();
file.write((char *)&_money,sizeof(_money));
//保存备注
len=veccon[i].GetComment().length();
file.write((char *)&len,sizeof(len));
file.write(veccon[i].GetComment().c_str(),len);
}
//充值记录备份
vector<CAppMoney> vecapp=appman.GetVecMoneyList();
float money;
count=vecapp.size();
file.write((char *)&count,sizeof(count));
for (i=0;i<count;i++)
{
//保存id
id=vecapp[i].GetID();
file.write((char *)&id,sizeof(id));
//保存姓名
len=vecapp[i].GetName().length();
file.write((char *)&len,sizeof(len));
file.write(vecapp[i].GetName().c_str(),len);
//保存日期
len=vecapp[i].GetDate().length();
file.write((char *)&len,sizeof(len));
file.write(vecapp[i].GetDate().c_str(),len);
//保存充值金额
money=vecapp[i].GetMoney();
file.write((char *)&money,sizeof(money));
//保存备注
len=vecapp[i].GetComment().length();
file.write((char *)&len,sizeof(len));
file.write(vecapp[i].GetComment().c_str(),len);
}
file.close();
return true;
}
/************************************************************************
数据备份框架
************************************************************************/
bool CFormBackupData::BackupData()
{
//输入路径确认后,工作目录转到该文件夹
CFormBase::load();
string title("Backup data");
int len=title.length();
GotoXY((80-len)/2,3);
cout<<title<<endl;
GotoXY(30,15);
cout<<"Input the path : "<<ends;
string path;
GetString(path,50);
CMyString::CheckFoundFiles(path);
SaveData(2);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -