调试没问题了,就是运行的时候,运行的时候各个功能出问题,请大侠帮忙找找问题,作业要交啦!
[code#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define N 1
int k;
FILE*fp;
//建立函数结构体存储信息
struct person
{
int QQ;
int number; //序号
char name[10];
char sex[10];
int telephone; //手机号码
int hometele; //家庭电话
char email[10];
char address[30]; //通讯地址
int birthday;
};
//编写建立信息文件函数,从键盘输入N个人的信息,并转存到文件中
void mkfile()
{
FILE*fp;
int i=0,j=N;
struct person per[N];
if((fp=fopen("filename","w"))==NULL)
{
printf("can not open this file\n");
exit(0);
}
printf("请输入%d个人的以下信息:\n",N);
printf("\n\n\t\t======================================================\n");
for(i=0;i<N;i++)
{
printf("\n\t\t第%d位同学的学号:",i+1);
scanf("%d",&per[i].number);
printf("\t\t第%d位同学的姓名:",i+1);
scanf("%s",per[i].name);
printf("\t\t第%d位同学的性别:",i+1);
scanf("%s",per[i].sex);
printf("\t\t第%d位同学的电话号码:",i+1);
scanf("%d",&per[i].telephone);
printf("\t\t第%d位同学的QQ:",i+1);
scanf("%d",&per[i].QQ);
printf("\t\t第%d同学的家庭号码:",i+1);
scanf("%d",&per[i].hometele);
printf("\t\t第%d位同学的电子邮件:",i+1);
scanf("%s",per[i].email);
printf("\t\t第i位同学的通讯地址:");
scanf("%s",per[i].address);
printf("\t\t第%d位同学的出生日期:",i+1);
scanf("%d",&per[i].birthday);
fwrite(&per[N],sizeof(struct person),1,fp);
}
fclose(fp);
printf("\n\n\t\t文件保存成功!请按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
//显示函数
void show()
{
struct person per[N];
FILE*fp;
int i=0;
if((fp=fopen("filename","r"))!=NULL)
{
if(fread(&per[N],sizeof(struct person),1,fp)!=1)
{ printf("无法打开文件!");
exit(0);
}
else
{
for(i=0;i<=N;i++)
{
printf("\t\t%d%9s%9s",per[i].number,per[i].name,per[i].sex);
printf("\t\t%d\t\t%d\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele);
printf("\t\t%9s %9s ",per[i].email,per[i].address);
printf("\t\t%d",per[i].birthday);
}
fclose(fp);
printf("\n\n\t\t通讯录信息全部如上!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
}
else
{
printf("无法打开此文件!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
}
//建立查找函数
void seekInfo()
{
char seekname[10];
struct person per[N];
FILE*fp;
int i=0;
fread(&per[N],sizeof(struct person),1,fp);
if((fp=fopen("filename","r"))==NULL)
{ printf("can not open this file");
exit(0);
}
else
{
printf("please input seekname :");
scanf("%s",seekname);
for(i=0;i<=N;i++)
{
if(strcmp(per[i].name,seekname)==0) //实现查找功能的关键代码
{ printf("学号/姓名/性别/电话号码/家庭电话/QQ号码/电子邮件/通讯地址/出生日期\n");
printf("%d %s %s %d %d %d %s %s %d",per[i].number,per[i].name,per[i].sex,per[i].telephone,per[i].hometele,per[i].QQ,per[i].email,per[i].address,per[i].birthday);
printf("需查找信息如上!按任意键返回主菜单···");
scanf("%d",&k);
break ;
}
}
if(i==N+1)
{ printf("查无此人!请按任意键返回主菜单···");
scanf("%d",&k); printf("\n\n\t\t查无此人!按任意键返回主菜单···\n");
scanf("%d",&k);
return ;
}
return ;
fclose(fp);
}
}
//建立添加信息的函数
void addInfo()
{
struct person per[N];
static int i=0;
int j;
FILE*fp;
if((fp=fopen("filename","a"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
else
{ fread(&per[N],sizeof(struct person),1,fp);
printf("\n\t\t请输入需添加的信息:");
printf("\n\n\t\t*************************************\n");
printf("\n\t\t请输入学号:");
scanf("%d",&per[j].number);
printf("\t\t请输入姓名:");
scanf("%s",per[j].name);
printf("\t\t请输入性别:");
scanf("%s",per[j].sex);
printf("\t\t请输入电话号码:");
scanf("%d",&per[j].telephone);
printf("\t\t请输入QQ号:");
scanf("%d",&per[j].QQ);
printf("\t\t请输入家庭电话:");
scanf("%d",&per[j].hometele);
printf("\t\t请输入电子邮件:");
scanf("%s",per[j].email);
printf("\t\t请输入通讯地址:");
scanf("%s",per[j].address);
printf("\t\t请输入出生日期:");
scanf("%d",&per[j].birthday);
fwrite(&per[j],sizeof(struct person),1,fp);
fclose(fp);
printf("\t\t添加成功!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
}
//建立删除函数
void deletInfo()
{
FILE*fp;
struct person per[N];
char deletname[10];
int i=0;
int count=N;
if((fp=fopen("filename","w"))==NULL)
{
printf("can not open this file");
exit(0);
}
else
{
fread(&per[N],sizeof(struct person),1,fp);
printf("请输入需要删除信息的人名:");
scanf("%s",&deletname);
for(i=0;i<N;i++)
{
if(strcmp(deletname,per[i].name)==0)
{
per[i]=per[i+1]; //以向前赋值的方法实现个人信息的向前移动,进而删除了第i 个人的信息
count-=1; //通讯录删除某个信息后的人数
printf("删除信息后的通讯录:");
for(i=0;i<=count;i++)
{ printf("\t\t%d\t\t\t %-6s%-3s",per[i].number,per[i].name,per[i].sex);
printf("\t\t%d\t\t\t%d\t\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele);
printf("%-9s%-9s",per[i].email,per[i].address);
printf("\t\t%d\n",per[i].birthday);
}
}
}
if(i==count+1)
{printf("\n\t\t查无此人!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
printf("\n");
}
}
//更新
void update()
{ FILE*fp;
struct person per[N];
char seekname[10];
int i=0;
char m;
if((fp=fopen("filename","wb+"))==NULL)
{
printf("can not open this file");
exit(0);
}
else
{fread(&per[N],sizeof(struct person),1,fp);
printf("\n\t\t请输入需修改信息的人名字:");
scanf("%s",seekname);
for(i=0;i<=N;i++)
{
if(strcmp(seekname,per[i].name)==0) break;
}
printf("\n\n\t\t%-9s%-9s%-9s%-9s%-9s%-9s%-9s%-9s%-9s\n","学号","姓名","性别","电话号码","QQ号码","家庭电话","电子邮件","通讯地址","出生日期");
printf("\t\t%d\t\t\t %-6s%-3s",per[i].number,per[i].name,per[i].sex);
printf("\t\t%d\t\t\t%d\t\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele);
printf("%-9s%-9s",per[i].email,per[i].address);
printf("\t\t%d\n",per[i].birthday);
printf("\n\n\t\t请选择需修改的信息项:");
printf("\n\t\ta----学号");
printf("\n\t\tb----性别");
printf("\n\t\tc----电话号码");
printf("\n\t\td----QQ号码");
printf("\n\t\te----家庭电话");
printf("\n\t\tf----电子邮件");
printf("\n\t\tg----通讯地址");
printf("\n\t\th----出生日期");
printf("\t\t请选择:");
scanf("%s",&m);
loop: switch(m)
{
case 'a':printf("\n\t\t请输入新的学号:");
scanf("%d",&per[i].number);break;
case 'b':printf("\n\t\t请输入新的性别:");
scanf("%s",per[i].sex);break;
case 'c':printf("\n\t\t请输入新的电话号码:");
scanf("%d",&per[i].telephone);break;
case 'd':printf("\n\t\t请输入新的QQ号码:");
scanf("%d",&per[i].QQ);break;
case 'e':printf("\n\t\t请输入新的家庭电话:");
scanf("%d",&per[i].hometele);break;
case 'f':printf("\n\t\t请输入新的电子邮件:");
scanf("%s",per[i].email);break;
case 'g':printf("\n\t\t请输入新的通讯地址:");
scanf("%s",per[i].address);break;
case 'h':printf("\n\t\t请输入新的出生日期:");
scanf("%d",&per[i].birthday );
default :printf("\n\t\t选择错误,请重新选择:");
scanf("%s",&m);goto loop;
fwrite(&per[N],sizeof(struct person),1,fp);
printf("\n\t\t信息更新成功!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
if(i==N+1)
{
printf("\n\n\t\t查无此人!按任意键返回主菜单···");
scanf("%d",&k);
return ;
}
}
}
int main()
{
while(1)
{
printf("\n\n\t\t*****************************************\n");
printf("\n\n\t\t@¥¥欢迎使用此通讯录管理系统¥¥@\n");
printf("\n");
printf(" 1----创建通讯录\n");
printf(" 2----浏览通讯录\n");
printf(" 3----查找通讯录信息\n");
printf(" 4----添加通讯录信息\n");
printf(" 5----删除通讯录\n");
printf(" 6----更新通讯录\n");
printf(" 7----退出系统\n");
printf("\n\n\t\t******************************************\n");
printf(" 请选择:");
scanf("%d",&k);
switch(k)
{
case 1: mkfile();
break;
case 2:show();
break;
case 3:seekInfo();
break;
case 4:addInfo();
break;
case 5:deletInfo();
break;
case 6:update();
break;
case 7: return 0 ;
default:printf("\t\t选择错误!请重新选择:");
scanf("%d",&k);
}
}
return 0;
}
][/code]