How to handle text files to save scores of a game?

0

The idea is that when the user dies he enters his name and it is saved along with his score keeping the names and scores that were already saved. the problem is that the code I have only overwrites the previous file and saves the last name entered. Probe opening the file and roasting all the data inside it to an auxiliary string but the only way to read data from a text file that I know is .getline () and this function uses char instead of string

this is the code:

when the player dies, the name is entered.

    if (aircraft.isDead(false)){
        bool flag = false;

        text.setString("Juego Terminado \n Ingresa tu nombre:");
        text.setPosition(200, 200);

        if (event.type == sf::Event::TextEntered){
            if (event.text.unicode < 128 && event.text.unicode != 13){
                name.push_back(static_cast<char>(event.text.unicode));
                playerName.setPosition(200, 300);
                playerName.setString(name);
            }
            if (event.text.unicode == 13){
                Save(name);

            }
        }
    }

Save function:

void Game::Save(std::string nombre){
std::ofstream archivo("Puntajes.txt");

if (archivo.is_open()){
    aircraft.saveScore(archivo, nombre);
}
archivo.close();
}

the saveScore function of the aircraft class:

void Aircraft::saveScore(std::ofstream &archivo, std::string nombre){
archivo << '\n'+nombre+": "+scorestr;
}
    
asked by facundo rotger 15.06.2017 в 03:59
source

1 answer

1

Problem.

You have to open the file in add data mode. The default file opening mode is overwriting.

You would not have this problem if you had read the available documentation about the std::ofstream utility, so the solution would be ...

Solution.

Read the available documentation on the std::ofstream

utility.

Description of the problem, step by step.

Class std::ofstream is an abstraction of family data output stream std::ostream (same as std::cout )

Constructor .

std::ofstream specializes in sending data to file, has 7 constructors (one of them deleted and three of them dependent on std::filesystem of C ++ 17) but only one of them is relevant to this question:

  • explicit basic_ofstream( const char* filename, std::ios_base::openmode mode = ios_base::out );

    Create an output flow of data to file pointing to the file identified by the first parameter, if nothing is specified the second parameter gets the value std::ios_base_out .

Opening modes .

The second parameter of the constructor you are using specifies the opening modes of the file, it is a parameter of type std::ios_base::openmode . There are six opening modes available but only one of them is suitable for your needs:

  • std::ios_base::app Means add ( app end). After opening the file places the writing pointer at the end of it.

Proposal.

Change your Game::Save function as follows:

void Game::Save(std::string nombre){
    if (std::ofstream archivo{"Puntajes.txt", std::ios_base::app}){
        aircraft.saveScore(archivo, nombre);
    }
}

More details on how the above code works in this question .

    
answered by 15.06.2017 / 09:19
source