好吧,我有这两个结构,我把它们发送到一个函数保存到一个txt文件。将结构中的信息写入文件
struct Cost
{
double hours;
double cost;
double costFood;
double costSupplies;
};
struct Creatures
{
char name[50];
char description[200];
double length;
double height;
char location[100];
bool dangerous;
Cost management;
};
这是IM功能上混淆的部分,我不知道如何利用这种结构的每一行,并将其写入文件。有人可以向我解释如何做到这一点?
file.open(fileName, ios::out);
if (!file)
{
cout << fileName << " could not be opened." << endl << endl;
}
else
{
fileName << c.name
<< c.description
<< c.lenght
<< c.height
<< c.location
<< c.dangerious
<< c.management.hours
<< c.management.cost
<< c.management.costFood
<< c.management.costSupplies;
file.close();
cout << "Your creatures where successfully save to the " << fileName << " file." << endl << endl
<< "GOODBYE!" << endl << endl;
}
}
你将不得不为运算符<<为你的上述两个结构。这样你就可以在你的程序中使用它。 –
编辑为我认为你的意思。 – Baalzamon