【发布时间】:2013-11-18 17:19:30
【问题描述】:
我有两个问题。我正在处理基于文件的问题。
这是我的第一个程序。
ofstream outRegister( "account.dat", ios::out );
if ( !outRegister ) {
cerr << "File could not be opened" << endl;
exit( 1 );}
cout<<"enter your username :";
cin>>a;
cout<<"enter your password :";
cin>>b;
outRegister<<a<<' '<<b;
cout<<"your account has been created";
每次我运行此代码时,我的程序都会从用户那里获取数据并存储在“account.dat”文件中,但会覆盖以前的数据。我如何编写我的程序以将它们写在下一行?
我的第二个问题是,
每当我需要登录时,我都需要我的程序从“account.dat”文件中搜索用户提供的特定用户名和密码。如果匹配,则应允许访问。
ifstream inRegister( "account.dat", ios::in );
if ( !inRegister ) {
cerr << "File could not be opened" << endl;
exit( 1 );
}
string a,a1,b,b1;
cout<<"\nyou are a premium user\n\n"<<endl;
cout<<"\n enter your user name:\t";
cin>>a;
while(getline(inRegister,a1))
{
if(a1==a)
{
cout<<"\n enter your password: \t";
cin>>b;
inRegister>>b1;
if(b1==b)
{
cout<<"\n access granted logging in....\n\n";
Allowaccess();
}
else
{
cout<<"\n you have entered a wrong password";
}
}
else
cout<<"\n no such user name is found";
}
我的第二个编码正确吗?如果不是,谁能指导我如何正确使用getline函数?
【问题讨论】: