/////////////////////////////////////////////////////////////
//老師給我們分布里一個作業,讓我們定義一個怪物的結構體,
//輸入你想創建的數字就可以創建出相應個數的怪物。
//然后再輸入每個怪物的信息並保存到一個文本文件里面,以供以后讀取。
//這里我定義了一個結構體,還進行了二進制文件讀寫和存儲fread,fwrite
//這里只是一個createMonster()函數,在main函數里面調用即可。
struct Monster
{
int ID;
char name[10];
char species[10];
char tpye[10];
};
void createMonster(int n)
{
Monster *monster;
Monster *ms;
for (int i = 0;i<n;i++)
{
monster = new Monster[n];
ms = new Monster[n];
}
cout<<"創建怪物成功!"<<endl;
cout<<"設置怪物的數據:"<<endl;
cout<<endl;
for (int i = 0;i<n;i++)
{
cout<<"第"<<i<<"個怪物信息:"<<endl;
cout<<"ID "<<"名字 "<<"種族: "<<"戰斗類型"<<endl;
cin>>monster[i].ID>>monster[i].name>>monster[i].species>>monster[i].tpye;
}
cout<<"輸入完畢!准備保存怪物信息........"<<endl;
Sleep(10);
FILE* pf;
if ((pf = fopen("Monster.txt","wb+"))==NULL)
{
cout<<"不能打開文件!"<<endl;
}
fwrite(monster,sizeof(Monster),n,pf);
rewind(pf);
fread(ms,sizeof(Monster),n,pf);
for (int i = 0;i<n;i++)
{
cout<<ms[i].ID<<endl;
cout<<ms[i].name<<endl;
cout<<ms[i].species<<endl;
cout<<ms[i].tpye<<endl;
}
fclose(pf);
}