教えてください
Posted: 2015年12月02日(水) 00:45
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILENAME_PLAYERDATASAVE "save.txt"
#define FILENAME_PLAYERDATALOAD "load.txt"
#define PLAYER_MAX (2)
typedef struct
{
char name[21];
int hp;
int attack;
}PLAYER;
void input_player(PLAYER *);
void output_player(PLAYER *);
bool battle(PLAYER*, PLAYER*);
void SaveData(PLAYER*);
void LoadData(PLAYER*);
int main(void)
{
PLAYER player_data[PLAYER_MAX];
PLAYER *pt_player_data;
int i;
int b_end;
int turn;
pt_player_data = &player_data[0];
for (i = 0; i < PLAYER_MAX; i++)
{
input_player(pt_player_data);
pt_player_data++;
}
pt_player_data = &player_data[0];
for (i = 0; i < PLAYER_MAX; i++)
{
output_player(pt_player_data);
pt_player_data++;
}
turn = 0;
do{
b_end = battle(&player_data[turn], &player_data[(turn ^ 1)]);
turn = (turn ^ 1);
} while (!b_end);
getchar();
return 0;
}
void input_player(PLAYER*pt_data)
{
scanf("%s", pt_data->name);
rewind(stdin);
scanf("%d", &pt_data->hp);
rewind(stdin);
scanf("%d", &pt_data->attack);
rewind(stdin);
}
void output_player(PLAYER *pt_data)
{
printf("%s\n", pt_data->name);
printf("%d\n", pt_data->hp);
printf("%d\n", pt_data->attack);
}
bool battle(PLAYER *pt_atc, PLAYER *pt_def)
{
pt_def->hp -= pt_atc->attack;
if (pt_def->hp < 0)
pt_def = 0;
if (pt_def <= 0){
printf("%sの勝利\n", pt_atc->name);
return true;
}
return false;
}
void LoadData(PLAYER*player_data)
{
FILE *fp;
fp = fopen(FILENAME_PLAYERDATALOAD, "rb");
if (fp == NULL)
{
printf("ファイルオープンエラー");
exit(-1);
}
for (int i = 0; i < PLAYER_MAX; i++)
{
fread(player_data, sizeof(PLAYER), PLAYER_MAX, fp);
player_data++;
}
fclose(fp);
}
void SaveData(PLAYER*player_data)
{
FILE *fp;
fp = fopen(FILENAME_PLAYERDATASAVE, "wb");
if (fp == NULL)
{
printf("ファイルオープンエラー");
exit(-1);
}
for (int i = 0; i < PLAYER_MAX; i++)
{
fwrite(player_data, sizeof(PLAYER), PLAYER_MAX, fp);
player_data++;
}
fclose(fp);
}
バイナリデータの読み込み書き込みが分かりません
ソースを教えてくださいお願いします。