Q:

How to store old txt lines and store new output to new line on iostream (C++)

To begin with C++, I prepare a user registration and log-in program.The program I write records the information into the file.But when I open the program again, it wipes out the old record and re-writes it back.
I tried to define string and get input from text but it failed.
using namespace std;
void saveuser() {
    string datausername = "test";//User name to save or read from txt
    string datapassword = "pass";//password to save or read from txt
    string datarealname = "realname";//Real name of the user
    ofstream database;
    database.open ("userdatabase.txt");
    database << datausername.c_str();
    database << " , ";
    database << datapassword.c_str();
    database << " , ";
    database << datarealname.c_str();
    database.close();
}

I want to store all users information on separate lines.
Like, User1's details : User1 , Pass1 , UserName1
and in other line       User2 , Pass2 , UserName2

A:

Sounds like you would like to append to the file. 
To append to a file, pass the mode argument of std::ofstream::app into the open function.
Example:
void saveuser() {
    ....
    ofstream database;
    database.open ("userdatabase.txt", std::ofstream::out, | std::ofstream::app);
    ...
}

This will not overwrite the previous contents of the file.

