File confusion

I have a fairly big (for me) text rpg that I am making, and I want to let the player save and load their progress. I have a save function which creates a file and then writes all needed data to that file. This data includes the characters name, their attributes, contents of inventory, gold, level, etc. Unfortunately, functions like read() and getline() don't work for loading that file, because they read characters, while I have strings (which can be converted to and from c-style strings) and numbers.

The save function looks something like this:
1
2
3
4
5
6
7
8
9
void save()
{
     ofstream saveFile( "Save.txt" );
    
     saveFile << name << endl;
     saveFile << health << endl;
     saveFile << stamina << endl;
     //etc., etc., following same format
}


Load() looks like this
1
2
3
4
5
6
7
8
{
     ifstream saveFile( "Save.txt" );
     
     saveFile >> name;
     saveFile >> health;
     saveFile >> stamina;
     //etc., etc., following the format, in the order these variables were saved in
}

I am getting a logic error: when I load the file, it confuses some variables, especially annoying is when it puts weapons/armor into spells or skills (casting leather helmet for 23 damage is terribly annoying), even though I quintuple to the power of the american national debt checked to make sure the order and the variables are all the same. I can provide the functions in their entirety, but they are quite long, because there is a lot to save and load.

Thanks in advance for any advice!
Your issue is spaces in the names of things - you need to format the file so there is no ambiguity.
I do, though, with endl statements. It still doesn't work. Not to mention, seeing as the last thing the load function loads is my vectors, having no data to read should result in a crash, which doesn't happen. The program goes on with spells like "leather helmet".
When you use the formatted input operator >> it only reads until the next space. You need to use std::getline.
Last edited on
Dang. So an endl doesn't count as a space? How do I use getline() with my numbers (like level, health, etc.)? It reads characters, shouldn't it not read numbers?
You can read it as a string then convert it to an integer with std::stoi.
It's not like a newline isn't a space, but what if the name contains a space?
Also check if the file exists before attempting to read from it.
Last edited on
closed account (j3Rz8vqX)
Can something like this be a possibility?
1
2
3
4
5
6
7
8
9
10
11
void save_to_file(vector<string> game_name, vector<string> game_path,int number_of_games,string game_data)
{
    cout<<"\n===== Auto Save " << game_data << " ====="<<endl;
    fstream file(game_data.c_str(),ios::out|ios::trunc);
    for(int i=0;i<number_of_games;i++)
    {
        file << game_name[i] << '\n';
        file << game_path[i] << '\n';
    }
    file.close();
}


In this example. I've read data back from a file as string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void load_from_file(vector<string> &game_name, vector<string> &game_path,int &number_of_games,string game_data)
{
    cout<<"\n===== Auto Load " << game_data << " ====="<<endl;
    fstream file(game_data.c_str(), ios::in);
    if(file.bad())
        cout<<game_data<<" does not exist - file will be created at exit"<<endl;
    string s;
    do
    {
        getline(file,s);
        if(s == "")
            break;
        game_name.push_back(s);
        getline(file,s);
        if(s == "")
            break;
        game_path.push_back(s);
        number_of_games++;
    }while(true);
    file.close();
}


Some basic file input/output that I recently implemented for fun:
http://www.cplusplus.com/forum/beginner/120426/#msg657645

Have fun.
Last edited on
Muchas gracias!
Topic archived. No new replies allowed.