添加记录:
对于此功能,void read_data()函数已用于将银行记录添加到文件中。它要求输入诸如帐号,名字,姓氏和余额等信息。
显示/列表数据:
通过添加记录提供的信息,C ++中的银行记录系统项目中的void show_data()函数显示与特定账号,名字和姓氏相对应的记录。显示帐户持有者的当前余额。
搜索记录:
当此功能的功能首次执行时,它会显示文件中的全部记录,然后用户可以按记录编号进行搜索。如果未找到搜索到的记录,C ++中的银行记录系统项目会显示消息 - “Error in opening! File Not Found!!”
编辑记录:
这与搜索功能类似。编辑记录功能首次执行时,会显示文件中的全部记录,用户可以通过提供记录编号来编辑信息。然后,C ++项目显示该记录中的所有数据,并且用户可以输入任何要修改的数据。如果没有找到要编辑的记录,则会显示消息 - “Error in opening! File Not Found!!”
删除记录:
首先,执行该功能的功能时,会显示文件中的所有记录,用户可以输入要删除的记录编号。如果没有找到该记录,则该C ++中的银行记录系统项目会显示消息 - “Error in opening! File Not Found!!”
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
class account_query
{
private:
char account_number[20];
char firstName[10];
char lastName[10];
float total_Balance;
public:
void read_data();
void show_data();
void write_rec();
void read_rec();
void search_rec();
void edit_rec();
void delete_rec();
};
void account_query::read_data()
{
cout<<"\nEnter AccountNumber:";
cin>>account_number;
cout<<"Enter FirstName:";
cin>>firstName;
cout<<"Enter LastName:";
cin>>lastName;
cout<<"Enter Balance:";
cin>>total_Balance;
cout<<endl;
}
void account_query::show_data()
{
cout<<"Account Number:"<<account_number<<endl;
cout<<"FirstName:"<<firstName<<endl;
cout<<"LastName:"<<lastName<<endl;
cout<<"CurrentBalance:Rs."<<total_Balance<<endl;
cout<<"____________________________"<<endl;
}
void account_query::write_rec()
{
ofstream outfile;
outfile.open("record.bank",ios::binary|ios::app);
read_data();
outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
outfile.close();
}
void account_query::read_rec()
{
ifstream infile;
infile.open("record.bank",ios::binary);
if(!infile)
{
cout<<"Error in Opening!File Not Found!!"<<endl;
return;
}
cout<<"\n*****Data from file**********"<<endl;
while(!infile.eof())
{
if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0)
{
show_data();
}
}
infile.close();
}
void account_query::search_rec()
{
int n;
ifstream infile;
infile.open("record.bank",ios::binary);
if(!infile)
{
cout<<"\nError in Opening! File Not Found!!"<<endl;
return;
}
infile.seekg(0,ios::end);
int count= infile.tellg()/sizeof(*this);
cout<<"\n There are"<<cout<< "record in the file";
cout<<"\nEnter Record Number to Search:";
cin>>n;
infile.seekg((n-1)*sizeof(*this));
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
show_data();
}
void account_query::edit_rec()
{
int n;
fstream iofile;
iofile.open("record.bank",ios::in|ios::binary);
if(!iofile)
{
cout<<"\nError in opening! File Not Found!!"<<endl;
return;
}
iofile.seekg(0,ios::end);
int count=iofile.tellg()/sizeof(*this);
cout<<"\n There are"<<count<<"record in the file";
cout<<"\n Enter Record Number to edit:";
cin>>n;
iofile.seekg((n-1)*sizeof(*this));
iofile.read(reinterpret_cast<char*>(this),sizeof(*this));
cout<<"Record"<<n<<"has following data"<<endl;
show_data();
iofile.close();
iofile.open("record.bank",ios::out|ios::binary);
iofile.seekg((n-1)*sizeof(*this));
cout<<"\n Enter data to Modify"<<endl;
read_data();
iofile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
void account_query::delete_rec()
{
int n;
ifstream infile;
infile.open("record.bank",ios::binary);
if(!infile)
{
cout<<"\nError in Opening! File Not Found!!"<<endl;
return;
}
infile.seekg(0,ios::end);
int count=infile.tellg()/sizeof(*this);
cout<<"\n There are"<<count<<"record in the file";
cout<<"\n Enter Record Number to Delete:";
cin>>n;
fstream tmpfile;
tmpfile.open("tmpfile.bank",ios::out|ios::binary);
infile.seekg(0);
for(int i=0;i<count;i++)
{
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
if(i==(n-1))
continue;
tmpfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
infile.close();
tmpfile.close();
remove("record.bank");
rename("tmpfile.bank","record.bank");
}
int main()
{
account_query A;
int choice;
cout<<"***Acount Information System***"<<endl;
while(true)
{
cout<<"Select one option below";
cout<<"\n\t1-->Add record to file";
cout<<"\n\t2-->Show record from file";
cout<<"\n\t3-->Search Record from file";
cout<<"\n\t4-->Update Record";
cout<<"\n\t5-->Delete Record";
cout<<"\n\t6-->Quit"<<endl;
cout<<"enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
A.write_rec();
break;
case 2:
A.read_rec();
break;
case 3:
A.search_rec();
break;
case 4:
A.edit_rec();
break;
case 5:
A.delete_rec();
break;
case 6:
exit(0);
break;
default:
cout<<"\nEnter corret choice";
exit(0);
}
}
system("pause");
return 0;
}
主菜单
数据输入:
显示系统储存的数据: