Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Input/output with files help  (Read 5180 times)

0 Members and 1 Guest are viewing this topic.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Input/output with files help
« on: September 18, 2012, 06:30:26 pm »
I am trying to save player data to a txt file in the follownig format, but the problem is that i dont know how to save a single stat(ex. playerclass), and i dont know how to load into variables. Help!!


void Engine::SaveCharacterData(string PlayerName, int PlayerGender, int PlayerClass, int PlayerSkin)
{
    ofstream myfile ("Player1.txt");
    if (myfile.is_open())
    {
        myfile <<"Name:"<< PlayerName;
        myfile <<"Gender:"<< PlayerGender;
        myfile <<"Class:"<< PlayerClass;
        myfile <<"Skin:"<< PlayerSkin;
        myfile.close();
    }
    else
        cout << "Unable to open file";
}

void Engine::SaveCharacterClass(int PlayerClass)
{
    string curLine;

    ofstream myfile ("Resources/Saved_data/Player1.txt");
    if (myfile.is_open())
    {
        getline(myfile, curLine);
        if(curLine == "Class=")
        {
            myfile << PlayerClass;
        }
        myfile.close();
    }
    else
        cout << "Unable to open file";
}

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Input/output with files help
« Reply #1 on: September 18, 2012, 06:35:14 pm »
What is exactly the problem and why doesn't it have anything to do with sfml?
Back to C++ gamedev with SFML in May 2023

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Input/output with files help
« Reply #2 on: September 18, 2012, 06:39:12 pm »
I need someone to make me or tell me how to write to specif line.
Hm now that you say i doesnt really have anything with sfml, asides that im using sfml to make a game.


Please help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Input/output with files help
« Reply #3 on: September 18, 2012, 10:06:57 pm »
There are many ways to load and save data and each and everyone has to fit with the data.
We won't gonna code stuff for you.
You'll have to try it on your own or use existing solutions (google will help you).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Input/output with files help
« Reply #4 on: September 18, 2012, 10:11:20 pm »
You could create a struct to hold player data which you load from file at startup, modify during runtime, and write out to a file when necessary. This should be a good place to start.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Input/output with files help
« Reply #5 on: September 19, 2012, 07:51:26 am »
myfile <<"Name:"<< PlayerName;
myfile <<"Gender:"<< PlayerGender;
 

This code will create a file that looks like this
Name:GeorgeGender:10
 

When you save to the file, just insert a blank space or return character after the value, like this

myfile <<"Name:"<< PlayerName << std::endl;
myfile <<"Gender:"<< PlayerGender << std::endl;
 

Now the saved file will look like this
Name:George
Gender:10
 

To load data, just get the string from the ":" character to the end using substr
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

 

anything