#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 100
struct student
{
char num[10];
char name[10];
char tel[10];
};
void chuangjian();
void xianshi();
void shanchu();
main()
{
int choose;
clrscr();
//printf("\n\n\n\n\n\n");
while(1)
{
printf(" ----------------------------------\n");
printf(" | choose a number(0~3): |\n");
printf(" |--------------------------------|\n");
printf(" | 1---chuang jian |\n");
printf(" | 2---xian shi |\n");
printf(" | 3---shan chu |\n");
printf(" | 0---quit |\n");
printf(" |--------------------------------|\n");
printf(" choose a number:");
scanf("%d",&choose);
switch(choose)
{
case 1:chuangjian();break;
case 2:xianshi();break;
case 3:shanchu();break;
case 0:exit(0);
default: printf("error");
}
}
}
void chuangjian() /* 输入数据函数*/
{
int i,n=0;
struct student temp , stu[N];
FILE *fp;
for(i=0;i<N;i++)
{
printf("\n put the %d :\n",i+1);
printf(" num(end with#):"); scanf("%s",&stu[i].num);
printf(" name(end with #):"); scanf("%s",&stu[i].name);
printf(" tel(end with #):"); scanf("%s",&stu[i].tel);
if(stu[i].num[0]=='#' || stu[i].name[0]=='#' ||
stu[i].tel[0]=='#')
break;
n++;
}
fp=fopen("tongxun.txt","wb");
if(fp==NULL)
{
printf("\n error!\n"); return;
}
for(i=0;i<n;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf(" error!\n");
fclose(fp);
}
void xianshi() /*输出函数*/
{
struct student temp;
FILE *fp;
int i,len,n;
fp=fopen("tongxun.txt","rb");
if(fp==NULL)
{
printf("\n cannotopen!\n");
return;
}
clrscr();
printf(" num name tel\n");
fseek(fp,0,SEEK_END);
len=ftell(fp);
n=len/sizeof(struct student);
rewind(fp);
for(i=0;i<n;i++)
{
fread(&temp,sizeof(struct student),1,fp);
printf("%10s%10s%10s\n",temp.num , temp.name, temp.tel);
}
fclose(fp);
}
void shanchu() /*删除函数*/
{
char tempnum[10];
FILE *fp;
int n=0;
struct student record[N],*p,*k;
fp=fopen("tongxun.txt","rb");
p=record;
while(feof(fp)==0)
{
fread(p,sizeof(struct student),1,fp) ;
p++;
n++;
}
fclose(fp);
printf("\n put the num which you will delete:");
getchar();
gets(tempnum);
for(k=record;k<record+n;k++)
if(strcmp(tempnum , k->num)==0) break;
if(k<record+n)
for(p=k;p<k+n;p++)
*p=*(p+1);
else
printf("\n there is not the people!\n");
fp=fopen("tongxun.txt","wb");
if(fp==NULL)
{
printf("\n cannot open!\n");
return;
}
for(p=record;p<record+n-1;p++)
fwrite(p,sizeof(struct student),1,fp);
fclose(fp);
}
添加回答
举报
0/150
提交
取消