#define null NULL
#define max_data 3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int x,
y;
}data_t;
data_t data[] = {{5, 5}, {3,4}, {8, 1}};//最大3個
void show(){
for(int i=0;i<max_data;i++){
printf("data[%d] = (%d, %d)\n", i, data[i].x, data[i].y);
}
printf("\n");
}
int main(){
//関数のプロトタイプ宣言
void ini();
void set();
void file_writer(const char*);
void file_reader(const char*);
//処理
show();
printf("ファイルに書き込み\n"); file_writer("data01.dat");
printf("\n初期化\n");
ini(); show();
printf("ファイルから読み込み\n"); file_reader("data01.dat");
show();
return 0;
}
void ini(){
memset(data, 0, sizeof(data_t)*max_data);
}
void file_writer(const char* fname){
FILE *fp;
if( (fp = fopen(fname, "wb")) == null){
printf("ファイル作成に失敗しました");
fclose(fp);
return;
}
fwrite(data, sizeof(data_t)*max_data, 1, fp);
fclose(fp);
}
void file_reader(const char* fname){
FILE *fp;
if( (fp = fopen(fname, "rb")) == null){
printf("ファイル読み込みに失敗しました");
fclose(fp);
return;
}
fread(data, sizeof(data_t)*max_data, 1, fp);
fclose(fp);
}
I2RlZmluZSBudWxsIE5VTEwKI2RlZmluZSBtYXhfZGF0YSAzCgojaW5jbHVkZTxzdGRpby5oPgojaW5jbHVkZTxzdGRsaWIuaD4KI2luY2x1ZGU8c3RyaW5nLmg+Cgp0eXBlZGVmIHN0cnVjdHsKCWludCB4LAoJICAgIHk7Cn1kYXRhX3Q7CgpkYXRhX3QgZGF0YVtdID0ge3s1LCA1fSwgezMsNH0sIHs4LCAxfX07Ly/mnIDlpKfvvJPlgIsKCgp2b2lkIHNob3coKXsKCWZvcihpbnQgaT0wO2k8bWF4X2RhdGE7aSsrKXsKCQlwcmludGYoImRhdGFbJWRdID0gKCVkLCAlZClcbiIsIGksIGRhdGFbaV0ueCwgZGF0YVtpXS55KTsKCX0KCXByaW50ZigiXG4iKTsKfQppbnQgbWFpbigpewoJLy/plqLmlbDjga7jg5fjg63jg4jjgr/jgqTjg5flrqPoqIAKCX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