You COULD create a pos_type for every entry, but that would be foolish. Take Wraithan's advice: read it all into memory, and then deal with it. When you're done, write it back.
save file:
Code:
Bob|Paladin|500|500|George|Necromancer|230|750|
I might...
Put it all in memory, then make a vector of the entries between |'s.
Use that for data.
Something like this:
Code:
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
namespace saving //namespaces = tidy code (when not excessive)
{
namespace file_settings
{
const char delim = '|';
}
bool load(const std::string & path,std::vector<std::string> & parsed_out)
{
std::ifstream saved_f(path.c_str());
//check stream for validity
if(!saved_f) return false; //however you deal with this
std::stringstream file_data;
char ch;
while(saved_file.get(ch)) file_data.put(ch); //copy streams, char for char
//keep in mind that file_data's get pointer is still at 0
std::string entry;
while( std::getline(file_data,entry,file_settings::delim) ) parse_out.push_back(entry); //parse input
return true;
//ifstream destructor is called, so close() is called
}
bool save(const std::string & path,std::vector<std::string> & parsed_in)
{
std::ofstream saved_f(path.c_str());
//check stream for validity
if(!saved_f) return false; //however you deal with this
//put it back out, with separator
for(int x = 0; x < parsed_in.size(); x++) saved_f << parsed_in[x] << file_settings::delim;
return true;
}
} //namespace
int main()
{
std::string path = "C:\\game\\saved\\001.sav";
std::vector<std::string> saved_info;
if(!saving::open(path,saved_info))
{
std::cerr << "open failure" << std::endl;
//use defaults
}
//..... do all sorts of things... run a game.....
//...just modify saved_info...
//..then, whenever user wants to REALLY save
if(!saving::save(path,saved_info)) std::cerr << "save failure" << std::endl;
//......
return 0;
}
Also, something like this might come in handy:
Code:
namespace specs
{
const int num = 4;
enum { NAME, CLASS, HP, MP };
const char* names[num] = {"name","class","hp","mp"};
}
Then you could display the contents of the file:
Code:
//given std::vector<std:string> info
for(int y = 0; y < info.size(); y++)
{
if(y%specs::num == 0 && y != 0) std::cout << std::endl; //newline after every profile
std::cout << specs::names[y%specs::num] << ": " << info[y] << std::endl;
}
So, should your program change what sort of info it saves (as it might), you only have to change these settings namespaces, rather than all of your code.
Anyway, hope I was a help.
PS: If anyone sees anything wrong with my idea or code, please do correct me. I'm just learning, too.