/*=============Program   Description================*/
/*  最唗靡備ㄩxxgl.c                                */
/*  最唗醴腔ㄩ勤悝汜價掛陓洘輛俴奪燴睿紱釬          */
/*  釬氪:     燠踢倯                                */
/*==================================================*/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
FILE *fp;
int i;
char filename[10];

/*隅砱珨跺賦凳极曹講*/
struct student
 {
     int number;
     char name[20];
     char sex;
     int age;
     char address[40];
 }st[60];
 /*={{0,'','',0,''},{0,'','',0,''},{0,'','',0,''}};   */

/*滲杅啎隅砱*/
 int menu_select();
 int newf();
 void openf();
 void closef();
 void savef();
 void save_asf();
 void search();


/*粕等滲杅*/
int menu_select()
{
  char s[80];
  int c;
  clrscr();
  putchar('\n');
  printf("庚岸岸岸岸岸岸岸岸岸岸岸岸岸庖 \n");
  printf("岫   悝汜陓洘奪燴炵苀敦諳   岫 \n");
  printf("岫                          岫 \n");
  printf("岫         1.new            岫 \n");
  printf("岫         2.open           岫 \n");
  printf("岫         3.close          岫 \n");
  printf("岫         4.save           岫 \n");
  printf("岫         5.save as        岫 \n");
  printf("岫         6.search         岫 \n");
  printf("岫         7.exit           岫 \n");
  printf("弩岸岸岸岸岸岸岸岸岸岸岸岸岸彼 \n");

   do{
      printf("\nEnter you choice:");
      gets(s);
      c=atoi(s); /*atoi()蔚蚕垀硌砃腔趼睫揹蛌遙峈淕倰(int)硉.滲杅埻倰婓stdlib.h笢*/
      }while(c<0||c>7);
   return c;
 }


/*NEW滲杅*/
newf()
{
  char c,s;
  printf("Input new file name:");
  scanf("%s",filename);
  fp=fopen(filename,"w+");
  printf("Input file data:\n");
  for(i=0;;i++)
  {
    printf("Please Input the data(number,sex,age)\n:");
    scanf("%d %s %c %d %s",&st[i].number,st[i].name,&st[i].sex,&st[i].age,st[i].address);
   /* printf("Please Input the name:");
    scanf("%s",st[i].name);
    printf("please input the address:");
    scanf("%s",st[i].address); */
    printf("SAVE?(Y/N)");
    scanf("%s",&s);
    if(s=='y'||s=='Y') savef();
    printf("\ncontinue?(Y/N)");
    getchar();
    scanf("%c",&c);
    if(c=='y'||c=='Y') continue;
    else break;
   }
   closef();
}

/*OPEN滲杅*/
void openf()

 {

  printf("Input the file name:");
  scanf("%s",filename);
  if((fp=fopen(filename,"r+"))==NULL)
    {
       printf("Can't open the file!!!");
       exit(0);
    }
   for(i=0;;i++)
      {
	fread(&st,sizeof(struct student),1,fp);
	if(feof(fp)) break;
	printf("%d,%c,%d,",st[i].number,st[i].sex,st[i].age);
	printf("%s\n",st[i].name);
	printf("%s\n",st[i].address);
      }
   getchar();
   getchar();
}

/*CLOSE滲杅*/
void closef()
{
   int j;
   char s;
   printf("file will be closed,save or not ? y/n ");
   scanf("%c",&s);
   if (s=='y'||s=='Y')
    { printf("save------4\n");
      printf("save to---5\n");
      scanf("%d",&j);
      if(j==4)  savef();
      else save_asf();
      }
   fclose(fp);
   printf("file closed\n");
   getchar();
}

/*SAVE滲杅*/
void savef()
{
   if(fwrite(&st,sizeof(struct student),1,fp)!=1)
   printf("error!\n");
   printf("%d,%c,%d,%s,%s",st[i].number,st[i].sex,st[i].age,st[i].name,st[i].address);
}

/*SAVA AS滲杅*/
void save_asf()
{
   FILE *fp1;
   openf();
   printf("Please Input new filename:");
   scanf("%s",filename);
   fp1=fopen(filename,"w+");
   rewind(fp);
   for(i=0;;i++)
   {
      fread(&st,sizeof(struct student),1,fp);
      if(feof(fp)) break;
      fwrite(&st,sizeof(struct student),1,fp1);
   }
}

/*SEARCH滲杅*/
void search()
{
   int num;
   openf();
   printf("Please Input the number:\n");
   scanf("%d",&num);
   rewind(fp);
   for(i=0;;i++)
   {
     fread(&st,sizeof(struct student),1,fp);
     if(!feof(fp)&&(num==st[i].number))
       { printf("found!\n%d,%c,%d,%s,%s\n",st[i].number,st[i].sex,st[i].age,st[i].name,st[i].address);
	   getchar();
	   getchar();
	   break;}
     if(feof(fp)){printf("no found\n"); getchar();getchar();break;}
     }
}
 /*翋最唗耀輸*/
main()
{
  clrscr();    /*ь壺淕跺絞ヶ敦諳ㄛ甜參嫖梓隅弇婓酘奻褒ㄗ1ㄛ1ㄘ揭,滲杅埻倰婓conio.h笢*/
     for(;;)
      {
      switch(menu_select())  /*堍蚚嗣煦盓恁寁逄曆覃蚚粕等滲杅ㄛ俇傖粕等跪髡夔腔妗珋*/
       {
        case 1:
	       newf();break;
        case 2:
	       openf();break;
        case 3:
	       closef();break;
        case 4:
               savef();break;
        case 5:
               save_asf();break;
        case 6:
               search();break;
        case 7:
	       exit(0);
       }
   }
}
