记录

发表于:2007-05-26来源:作者:点击数: 标签:
#include stdio.h #include windows .h struct person { char name[8]; char tel[15]; char addr[50]; }; char filename[20]; FILE *fp; #include stdio.h #include windows.h struct person { char name[8]; char tel[15]; char addr[50]; }; char filename
#include <stdio.h>
#include <windows.h>
struct person
{
  char name[8];
  char tel[15];
  char addr[50];
};
char filename[20];
FILE *fp;

#include <stdio.h>
#include <windows.h>
struct person
{
  char name[8];
  char tel[15];
  char addr[50];
};
char filename[20];
FILE *fp;

void creat();
void output();
void search();
void append();
void modify();
void delete();

main()
{
  int m;
  creat();
  while(1)
  {
  printf("\n\n添加,请按1");
  printf("\n查找,请按2");
  printf("\n修改,请按3");
  printf("\n删除,请按4");
  printf("\n输出,请按5");
  printf("\n退出,请按0");
  printf("\n");
  scanf("%d",&m);
  if (m>=0&&m<=5)
  {
     switch(m)
     {
     case 1: append();
     break;
     case 2: search();
     break;
     case 3: modify();
     break;
     case 4: delete();
     break;
     case 5: output();
      break;
case 0: exit(fp);
  }
  printf("\n\n操作完成,请再次选择!");
  }
  else
  
  printf("\n\n选择错误,请再次选择!");
  }
}

void creat()
{
  struct person one;
  long s1;
   printf("*************************************************\n");
   printf("*   written by 刘歧  1jjk  *\n");
   printf("*************************************************\n");
  printf("\n请输入记事本名:");
  scanf("%s",filename);
  if ((fp=fopen(filename,"w"))==NULL)
  {
   printf("\n不能建立记事本");

   fprintf(fp,"%-10s%-20s%-50s","姓名","日期","事件");
   printf("\n请输入姓名,日期和事件以0结束\n");
   scanf("%s",one.name);
   while(strcmp(one.name,"0"))
   {
     scanf("%s%s",one.tel,one.addr);
     fprintf(fp,"%-10%s-20%s-50s",one.name,one.tel,one.addr);
     scanf("%s",one.name);
   }
   fclose(fp);
  }
}

void output()
{
  struct person one;
  if((fp=fopen(filename,"r"))==NULL)
  {
   printf("\n不能打开记事本!");

  }
  printf("\n\n%20s\n","记事本");
  while(!feof(fp))
  {
  fscanf(fp,"%-10%s-20%s-50s",one.name,one.tel,one.addr);
  printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
  }
  fclose(fp);
  
}
void append()
{
  struct person one;
  if ((fp=fopen(filename,"a"))==NULL)
  {
   printf("\n不能打开记事本!");


  }
  printf("\n请添入姓名,日期码及事件");
  scanf("%s%s%s",one.name,one.tel,one.addr);
  fprintf(fp,"%-10s%-20s%-50s",one.name,one.tel,one.addr);
  fclose(fp);
}

void search()
{
  int k=0;
  char namekey[8];
  struct person one;
  printf("\n请输入姓名");
  scanf("%s",namekey);
  if((fp=fopen(filename,"rb"))==NULL)
  {
   printf("\n不能打开记事本");

  }
  while (!feof(fp))
  {
   fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
   if(!strcmp(namekey,one.name))
   {
     printf("\n\n已经查到");
     printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
     k=1;
   }
  }
  if(!k)
   printf("\n\n对不起,记事本中没有这个人");
  fclose(fp);
}
void modify()
{
  int k=0;
  long offset;
  char namekey[8];
  struct person one;
  printf("\n请输入用户名");
  scanf("%s",namekey);
  if((fp=fopen(filename,"r+"))==NULL)
  {
   printf("\n不能打开记事本");
   
  }
  while(!feof(fp))
  {
   offset=ftell(fp);
   fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
   if(!strcmp(namekey,one.name))
   {
     k=1;
     break;
   }
  }
  if(k)
  {
   printf("\n已经查到,记录为:");
   printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
   printf("\n请输入新的姓名,日期和事件");
   scanf("%s%s%s",one.name,one.tel,one.addr);
   fseek(fp,offset,SEEK_SET);
   printf("%ld",ftell(fp));
   fprintf(fp,"%-10s%-20s%-50\n",one.name,one.tel,one.addr);
  }
  else
  {
   printf("\n对不起,记事本中没有这个人");
   fclose(fp);
  }
}

void delete()
{
  int m,k=0;
  long offset1;
  char namekey[8];
  struct person one;
  printf("\n请输入姓名");
  scanf("%s",namekey);
  if((fp=fopen(filename,"r+"))==NULL)
  {
  printf("\n不能打开记事本!");
   
  }
  while(!feof(fp))
  {
  offset1=ftell(fp);
  fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
  if(!strcmp(namekey,one.name))
  {
     k=1;
     break;
  }
  }
  if(k)
  {
  printf("\n已经查到,记录为:");
  printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);
  printf("\n确实要删除,按1,不删除,按0:");
  scanf("%d",&m);
  {
     if(m)
     {
      fseek(fp,offset1,SEEK_SET);
      fprintf(fp,"%-10s%-20s%-50s\n","","","");
     }
  }
  }
  else
  printf("\n对不起,记事本中没有此人");
  fclose(fp);
}

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)