www.gusucode.com > C++模拟ATM机智能存取系统源码程序 > C++模拟ATM机智能存取系统源码程序\code\CFile.cpp
//Download by http://www.NewXing.com #include "CFile.h" #include "CUserInfo.h" #include "CPersonOperatInfo.h" #include <fstream> #include <iostream> #include <string> #include <conio.h> using namespace std; C_File::C_File()//构造函数 { pUserInfo=NULL; pBalanceInfoData=NULL; pPersonOperatInfoData=NULL; } C_File::~C_File()//析构函数 { while(pUserInfo)//释放用户信息的内存 { CUserInfoData *ppUserInfo=NULL; ppUserInfo= pUserInfo->pNext; delete pUserInfo->user; delete pUserInfo; pUserInfo=ppUserInfo; } while (pBalanceInfoData)//释放帐户信息的内存 { CBalanceInfoData *ppBalanceInfoData=NULL; ppBalanceInfoData=pBalanceInfoData->pNext; delete pBalanceInfoData->UserBalanceInfo; delete pBalanceInfoData ; pBalanceInfoData=ppBalanceInfoData; } while (pPersonOperatInfoData)//释放个人操作信息的内存 { CPersonOperatInfoData* ppCurrentOperatInfo=NULL; ppCurrentOperatInfo=pPersonOperatInfoData->pNext; delete pPersonOperatInfoData->UserOperatInfo; delete pPersonOperatInfoData; pPersonOperatInfoData=ppCurrentOperatInfo; } } void C_File::ReadUserInfo()// 读取用户的个人信息文件 { int nLength=0; int i=0; string strUserCountNum1,strUserName1,strUserPasswd1,strCardID1,strAddress1,strPhone1; CUserInfoData *pCurrentUserInfoData=NULL; ifstream fin; fin.open("User.ini"); if (!fin.is_open()) { cout <<"file can not open!"<<endl; return; } while(!fin.eof()) { fin>>strUserCountNum1>>strUserName1>>strUserPasswd1 >>strCardID1>>strAddress1>>strPhone1; if (fin.fail()) { return ; } nLength=strUserCountNum1.length(); string strUserCountNum(&strUserCountNum1[1],nLength-2); long int nUserCountNum=atoi(strUserCountNum.c_str()); nLength=strUserName1.length(); string strUserName(&strUserName1[9],nLength-9); nLength=strUserPasswd1.length(); string strUserPasswd(&strUserPasswd1[11],nLength-11); nLength=strCardID1.length(); string strCardID(&strCardID1[7],nLength-7); nLength=strAddress1.length(); string strAddress(&strAddress1[8],nLength-8); nLength=strPhone1.length(); string strPhone(&strPhone1[10],nLength-10); CUserInfoData *pTailUserInfoData=new CUserInfoData; pTailUserInfoData->user=new CUserInfo(nUserCountNum,strUserName,strUserPasswd, strCardID,strAddress,strPhone); if (i==0) { pUserInfo=pCurrentUserInfoData=pTailUserInfoData; i++; } else { pCurrentUserInfoData->pNext=pTailUserInfoData; pCurrentUserInfoData=pTailUserInfoData; } pTailUserInfoData->pNext=NULL; } } void C_File::ReadBalanceInfo()// 读取用户的帐户信息 { CBalanceInfoData* pCurrentUserInfoData=NULL; int nLength=0; int i=0; string strUserCountNum, strPasswd,strBalance,strFlag; ifstream fin; fin.open("Money.txt"); if (!fin.is_open()) { cout <<"file can not open!"<<endl; return; } while(!fin.eof()) { fin>>strUserCountNum>>strPasswd>>strBalance>>strFlag; long int nUserCountNum=atoi(strUserCountNum.c_str()); int nFlag=atoi(strFlag.c_str()); if (fin.fail()) { return ; } CBalanceInfoData *pTailUserInfoData=new CBalanceInfoData; pTailUserInfoData->UserBalanceInfo=new CBalanceInfo(nUserCountNum,strPasswd,strBalance,nFlag); if (i==0) { pBalanceInfoData=pCurrentUserInfoData=pTailUserInfoData; i++; } else { pCurrentUserInfoData->pNext=pTailUserInfoData; pCurrentUserInfoData=pTailUserInfoData; } pTailUserInfoData->pNext=NULL; } return ; } void C_File::ReadPersonOperatInfo(char *pFileName)//读取用户的个人操作信息 { CPersonOperatInfoData* pCurrentOperatInfo=NULL; int nLength=0; int i=0; string strTime, strType, strMoney; ifstream fin; fin.open(pFileName); if (!fin.is_open()) { /* cout <<"file can not open!"<<endl;*/ return; } while (!fin.eof()) { fin>>strTime>>strType>>strMoney; int nType=atoi(strType.c_str()); if (fin.fail()) { return ; } CPersonOperatInfoData *pTailOperatInfo=new CPersonOperatInfoData; pTailOperatInfo->UserOperatInfo=new CPersonOperatInfo(strTime, nType, strMoney); if (i==0) { pPersonOperatInfoData=pCurrentOperatInfo=pTailOperatInfo; i++; } else { pCurrentOperatInfo->pNext=pTailOperatInfo; pCurrentOperatInfo=pTailOperatInfo; } pTailOperatInfo->pNext=NULL; } return ; } void C_File::SaveUserInfo()//保存用户的个人信息 { ofstream fout; fout.open("User.ini"); if (!fout.is_open()) { cout << "Creat File Failure!"; } CUserInfoData *ppUserInfo=NULL; ppUserInfo=pUserInfo; while(ppUserInfo) { fout << "[" << ppUserInfo->user->GetAccountNum()<< "]" <<endl; fout << "UserName=" << ppUserInfo->user->GetUserName() << endl; fout << "UserPasswd=" << ppUserInfo->user->GetUserPasswd() <<endl; fout << "CardID=" << ppUserInfo->user->GetCardID() <<endl; fout << "Address=" << ppUserInfo->user->GetAddress() <<endl; fout << "Telephone=" << ppUserInfo->user->GetPhone() <<endl; ppUserInfo = ppUserInfo->pNext; } fout.close(); } void C_File::SaveBalanceInfo()//保存用户的帐户信息 { ofstream fout; fout.open("Money.txt"); if (!fout.is_open()) { cout << "Creat File Failure!"; } CBalanceInfoData *ppBalanceInfoData=NULL; ppBalanceInfoData=pBalanceInfoData; while(ppBalanceInfoData) { fout<<ppBalanceInfoData->UserBalanceInfo->GetAccountNum()<<endl; fout<<ppBalanceInfoData->UserBalanceInfo->GetPassWd()<<endl; fout<<ppBalanceInfoData->UserBalanceInfo->GetBalance()<<endl; fout<<ppBalanceInfoData->UserBalanceInfo->GetFlag()<<endl; ppBalanceInfoData=ppBalanceInfoData->pNext; } fout.close(); } void C_File::SavePersonOperatInfo(char *pFileName)// 保存用户的个人操作信息 { ofstream fout; fout.open(pFileName); if (!fout.is_open()) { cout << "Creat File Failure!"; } CPersonOperatInfoData *ppPersonOperatInfoData=NULL; ppPersonOperatInfoData=pPersonOperatInfoData; while (ppPersonOperatInfoData) { fout<<ppPersonOperatInfoData->UserOperatInfo->GetTime()<<endl; fout<<ppPersonOperatInfoData->UserOperatInfo->GetTpype()<<endl; fout<<ppPersonOperatInfoData->UserOperatInfo->GetMoney()<<endl; ppPersonOperatInfoData=ppPersonOperatInfoData->pNext; } } long int C_File::FindMaxCardId()//获取系统的最大账号 { long int nMaxCarID=100000; CUserInfoData *ppUserInfo=NULL; ppUserInfo=pUserInfo; while(ppUserInfo) { if (ppUserInfo->user->GetAccountNum()>nMaxCarID) { nMaxCarID=ppUserInfo->user->GetAccountNum(); } ppUserInfo=ppUserInfo->pNext; } return nMaxCarID; } void C_File::ReadUserInfo(char *pName)//读取用户的个人信息文件 { int nLength=0; int i=0; string strUserCountNum1,strUserName1,strUserPasswd1,strCardID1,strAddress1,strPhone1; CUserInfoData *pCurrentUserInfoData=NULL; ifstream fin; fin.open(pName); if (!fin.is_open()) { cout <<"对不起没有发现该文件!"<<endl; return; } while(!fin.eof()) { fin>>strUserCountNum1>>strUserName1>>strUserPasswd1 >>strCardID1>>strAddress1>>strPhone1; if (fin.fail()) { return ; } nLength=strUserCountNum1.length(); string strUserCountNum(&strUserCountNum1[1],nLength-2); long int nUserCountNum=atoi(strUserCountNum.c_str()); nLength=strUserName1.length(); string strUserName(&strUserName1[9],nLength-9); nLength=strUserPasswd1.length(); string strUserPasswd(&strUserPasswd1[11],nLength-11); nLength=strCardID1.length(); string strCardID(&strCardID1[7],nLength-7); nLength=strAddress1.length(); string strAddress(&strAddress1[8],nLength-8); nLength=strPhone1.length(); string strPhone(&strPhone1[10],nLength-10); CUserInfoData *pTailUserInfoData=new CUserInfoData; pTailUserInfoData->user=new CUserInfo(nUserCountNum,strUserName,strUserPasswd, strCardID,strAddress,strPhone); if (i==0) { pUserInfo=pCurrentUserInfoData=pTailUserInfoData; i++; } else { pCurrentUserInfoData->pNext=pTailUserInfoData; pCurrentUserInfoData=pTailUserInfoData; } pTailUserInfoData->pNext=NULL; } } void C_File::SaveUserInfo(char *FileName)//保存用户的个人信息文件 { ofstream fout(FileName, ios::out|ios::app); if (!fout.is_open()) { cout<<"对不起没有发现配置文件!"<<endl; } CUserInfoData *ppUserInfo=NULL; ppUserInfo=pUserInfo; while(ppUserInfo) { fout << "[" << ppUserInfo->user->GetAccountNum()<< "]" <<endl; fout << "UserName=" << ppUserInfo->user->GetUserName() << endl; fout << "UserPasswd=" << ppUserInfo->user->GetUserPasswd() <<endl; fout << "CardID=" << ppUserInfo->user->GetCardID() <<endl; fout << "Address=" << ppUserInfo->user->GetAddress() <<endl; fout << "Telephone=" << ppUserInfo->user->GetPhone() <<endl; ppUserInfo = ppUserInfo->pNext; } fout.close(); }