#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
typedef struct
{
int Star_Score; //消灭星星分数常量 n*(n-1)*Satr_Score n>1
int D_Conversion; //钻石兑换比例 100/10 = 10
int LevelScore; //目标分数奖励
int Loginday[6]; //login天数 奖励
}GameData_Config;
fstream Out_bin_Config;
char *path = "c:\config.bin";
GameData_Config config;
void ShowData(GameData_Config *config)//定义输出函数
{
cout<<config->Star_Score<<endl;
cout<<"--------------------"<<endl;
cout<<config->D_Conversion<<endl;
cout<<"--------------------"<<endl;
cout<<config->LevelScore <<endl;
cout<<"--------------------"<<endl;
for (int i = 0; i < 7; i++)
{
cout<<config->Loginday[i]<<" ";
}
}
void GameDate_Config_init(int _score, int _conversion, int _LevelScore, int Login_jili)
{
config.Star_Score = _score;
config.D_Conversion = _conversion;
config.LevelScore = _LevelScore;
for (int i = 0; i < 7; i++)
{
config.Loginday[i] = Login_jili*(i + 1);
}
}
void write()
{
Out_bin_Config.open(path,ios::app | ios::binary); //以追加的方式写在文件末尾
if (!Out_bin_Config)
{
Out_bin_Config.write((char *)&config,sizeof(GameData_Config));
}
Out_bin_Config.close();
}
void read()
{
Out_bin_Config.open(path);
if (!Out_bin_Config.is_open())
{
cout<<"open file err";
exit(1);
}
while (!Out_bin_Config.eof())
{
Out_bin_Config.read((char *)&config,sizeof(GameData_Config));
}
ShowData(&config);
}
void main()
{
GameDate_Config_init(5,10,2000,2);
write();
read();
system("pause");
}
C++ 文件读写
最新推荐文章于 2022-07-29 16:46:44 发布