我正在尝试为程序创建一个加载函数。所以写了这个来保存我的信息。我真的不喜欢是否将每个竞争对手的所有内容保留在同一行,但这看起来最简单。
void GameHandler::WriteToFile()
{
ofstream file ("Competitors.txt");
if(file.is_open())
{
file<<this->mNrOfCompetitors<<"\n";
for(int i=0; i<this->mNrOfCompetitors; i++)
{
file<<this->mCompetitors[i].GetName()<<"\n";
file<<this->mCompetitors[i].GetDateOfBirth()<<"\n";
file<<this->mCompetitors[i].GetGender()<<"\n";
}
file.close();
}
else
{
cout<<"Unable to write to file."<<endl;
}
}
但现在当我必须加载所有内容时。人们拥有最基本的处理最简单案例的教程。这就是为什么我需要一些帮助。
我开始了,但这不起作用,因为我不知道输入中的线路开关如何。
void GameHandler::LoadFromFile()
{
cleanup(); //Erase current data and replace with loaded data.
string linestring;
int lineint;
bool linebool;
ifstream file ("Competitors.txt");
if(file.is_open())
{
file>>this->mNrOfCompetitors;
this->mCapacity = this->mNrOfCompetitors+5;
this->mCompetitors = new Duathlet[this->mCapacity];
for(int i=0; i<this->mNrOfCompetitors; i++)
{
file>>linestring;
this->mCompetitors[i].SetName(linestring);
//need lineswitch
file>>lineint;
this->mCompetitors[i].SetDateOfBirth(lineint);
//need lineswitch
file>>linebool;
this->mCompetitors[i].SetGender(linebool);
//need lineswitch
}
file.close();
}
else
{
cout<<"Unable to open file."<<endl;
}
}