#include <stdio.h>
#include <string.h>
/***************************************************************
▲ 名前、生年月日の構造体▲
***************************************************************/
struct data1 {
char name[64]; /* 名前 */
int year; /* 年 */
int manth; /* 月 */
int day; /* 日 */
int quantity; /* データ数 */
}birth[200];
/***************************************************************
■新規データの入力、データ編集用関数■
***************************************************************/
void inputdata(struct data1 birth[])
{
char name1[64];
printf("名前:"); scanf("%64s",name1); //名前を入力
strcpy(birth[ birth[0].quantity ].name , name1);
//birth構造体の quantity(データ数)に応じた場所へ入力
printf("誕生日の登録\n\n");
printf("西暦:"); scanf("%d",&birth[ birth[0].quantity ].year);
printf(" 月:"); scanf("%d",&birth[ birth[0].quantity ].manth);
printf(" 日:"); scanf("%d",&birth[ birth[0].quantity ].day);
birth[0].quantity+=1;
return 0;
}
/***************************************************************
■保存データ 一覧表示関数■
***************************************************************/
void indicate_data(void)
{
if(birth[0].quantity != 0 )
{
int repetition;
printf("名前 西暦 月 日\n");
for(repetition=0;repetition < birth[0].quantity; repetition++);
{
printf("%s" ,birth[repetition].name);
printf("%7d" ,birth[repetition].year);
printf(" %02d" ,birth[repetition].manth);
printf(" %02d\n\n" ,birth[repetition].day);
}
}else{
printf("データがありません\n"
"登録してから参照してください");
}
}
/***************************************************************
■バイナリデータへのデータ保存関数■
***************************************************************/
void write_data(void)
{
FILE *fp = fopen( "data.dat", "wb" );//バイナリファイルを開く
if( fp == NULL ){//エラーが起きたらNULLを返す
return 0;
}
fwrite( &birth, sizeof(birth), 1, fp ); // birth構造体の中身を出力
fclose( fp );//ファイルを閉じる
}
/***************************************************************
■バイナリデータ読み込み関数■
***************************************************************/
void read_data(void)
{
FILE *fp = fopen( "data.dat", "rb" );//バイナリファイルを開く
if( fp == NULL ){//エラーが起きたらNULLを返す
return 0;
}
fread( &birth, sizeof(birth), 1, fp ); // birth 構造体の中身を読み込み
fclose( fp );//ファイルを閉じる
}
/***************************************************************
MAIN
***************************************************************/
int main(void)
{
int menu=0;
while(1){
printf("データ読み込み中...\n");
system("cls");
read_data( birth ); //■バイナリデータ読み込み関数■
/***************************************************************
メインメニュー(分岐)
***************************************************************/
printf("新規データの入力 :1\n"
"データの編集 :2\n"
"アルファベット順名前一覧:3\n\n");
printf("入力:");
scanf_s( "%d", &menu ); //どういった動作を行うのか入力
printf("\n");
switch (menu){
case 1: // データの新規登録
inputdata( birth ); //■構造体への名前,生年月日入力関数■
write_data( birth ); //■バイナリデータへのデータ書き込み関数■
printf("\n登録が完了しました\n");
getch();
break;
case 2: // データの編集
break;
case 3: // 登録データ一覧表示
indicate_data( birth ); //■データ一覧表示関数■
getch();
break;
default: // 入力エラー
printf("1~3以外の値が入力されました\n");
break;
system("cls");
} //switch
} //while
return 0;
}
今現在私が困っているのは、二点あります。
一点目。
上から3つ目の大きな注釈、■保存データ 一覧表示関数■ のindicate_data関数で、登録されているデータを一覧表示するもの。
これが、データが正常に表示出来ません。
構造体配列の[ repetition ] を [0] と試しにしてみたところ、正常に [ 0 ]に保存されている名前、生年月日が一覧表示されました。
for文で repetition を 0に初期化しているのに、何故 repetition 変数を配列で使用すると内容が表示されなくなってしまうのでしょうか。
二点目なのですが、今後のプログラムの機能の追加についてなのですが
今現在は一覧表示の関数を作りましたが、これから、アルファベット順、月日順、誕生日まで一ヶ月以内の人、一週間以内の人一覧、などと、いろいろな表示方法を作る予定なのですが、 全てを関数化することしか私は考えられません。
main関数だけは綺麗にしておきたいのですが、皆様ならばどのように構成しますでしょうか。
やはり、プログラムの量が増えてきたからには、ソースファイルの分割を覚えるべきでしょうか。
回答いただけると幸いです。
よろしくお願いします。