简体   繁体   English

将字符串添加到文件而不覆盖以前的字符串

[英]Adding strings to files without overwriting the previous strings

I have two questions. 我有两个问题。 I m working on a problem based on files. 我正在研究基于文件的问题。

Here is my first program. 这是我的第一个程序。

    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";

everytime i run this code, my progrm gets data from user and stores in the "account.dat" file, but by overwriting the previous ones. 每次运行此代码时,我的progrm都会从用户获取数据并存储在“account.dat”文件中,但是会覆盖以前的数据。 How can i code my program to write them in the next line? 如何编写我的程序以在下一行编写它们?

And my second question is, 我的第二个问题是,

whenever i need to login, i need my program to search for that particular username and password given by user from the "account.dat" file.If it matches it should allow accesss. 每当我需要登录时,我都需要我的程序来搜索用户从“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";
}

is my second coding correct?If not, Can anyone guide me how to use the getline function properly? 我的第二个编码是否正确?如果没有,任何人都可以指导我如何正确使用getline功能吗?

Try using the append file mode app , whose behaviour is documented as 尝试使用追加文件模式app ,其行为记录为

All output operations happen at the end of the file, appending to its existing contents. 所有输出操作都发生在文件末尾,附加到其现有内容。

ofstream outRegister("account.dat", ios::app);

For your second question, try using ifstream and getline to process the file line by line, checking whether the first word in the line matches your target username. 对于第二个问题,请尝试使用ifstreamgetline逐行处理文件,检查行中的第一个单词是否与目标用户名匹配。

Try the second part yourself and post a question, including your code to date, if you get stuck. 如果您遇到问题,请自行尝试第二部分并发布一个问题,包括您的代码。

Try using the append file mode app, whose behaviour is documented as 尝试使用追加文件模式应用程序,其行为记录为

All output operations happen at the end of the file, appending to its existing contents. 所有输出操作都发生在文件末尾,附加到其现有内容。

ofstream outRegister("account.dat", ios::app);

you can use: 您可以使用:

std::ofstream outRegister("account.dat", std::ios_base::app | std::ios_base::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";

This code would would work just fine. 这段代码可以正常工作。 Because you are opening the file in append mode ( ios_base::app ) Actually the prototype of std::ofstream constructor is ( string, std::ios_base::openmode ) 因为你是以追加模式打开文件( ios_base::app )实际上std::ofstream构造函数的原型是( string, std::ios_base::openmode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM