小程序,希望各位高手指点
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct pnode
{
char name[8];
char tel[16];
char addr[30];
}personnode;
char filename[20];
FILE *fp;
void creat()
{
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n请输入通讯录名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\n没有输入通讯录名,不能建立通讯录!");
exit(0);
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
printf("\n请输入姓名,电话号码及住址(以空格隔开,以@结束)\n");
scanf("%s",person->name);
while(strcmp(person->name,"@"))
{
scanf("%s%s",person->tel,person->addr);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
scanf("%s",person->name);
}
fclose(fp);
}
void output()
{
personnode *person;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯录!");
exit(0);
}
printf("\n*********************************************\n");
printf("%24s\n","通讯录");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
}
fclose(fp);
printf("*********************************************\n\n");
}
void append()
{
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打开通讯录!");
exit(0);
}
printf("\n请输入添加的姓名,电话号码及住址\n");
scanf("%s%s%s",person->name,person->tel,person->addr);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
fclose(fp);
}
void search()
{
int k=0;
char namekey[8];
personnode *person;
printf("\n请输入需要修改记录的姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开通讯录!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
if(!strcmp(namekey,person->name))
{
printf("\n\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
k=1;
}
}
if(!k) printf("\n\n对不起,通讯录中没有此人的记录。\n");
fclose(fp);
}
void modify()
{
int k=0;
long offset;
char namekey[8];
personnode *person;
printf("\n请输入需要修改记录的姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯录!");
exit(0);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
if(!strcmp(namekey,person->name))
{
k=1;
break;
}
}
if(k)
{
printf("\n\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
printf("\n请输入新姓名,电话号码及住址:");
scanf("%s%s%s",person->name,person->tel,person->addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
}
else printf("\n对不起,通讯录中没有此人的记录。\n");
fclose(fp);
}
void delet()
{
int k=0;
char m;
long offset1;
char namekey[8];
personnode *person;
printf("\n请输入删除记录的姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯录!");
exit(0);
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->tel,person->addr);
if(!strcmp(namekey,person->name))
{
k=1;
break;
}
}
if(k)
{
printf("\n\n已查到,记录为:");
printf("%-10s%-20s%-50s\n",person->name,person->tel,person->addr);
printf("\n确实要删除?y/n");
scanf("%c",&m);
if(m='y')
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-14s%-24s%-48s\n"," "," "," ");
}
}
else printf("\n对不起,通讯录中没有此人的记录。\n");
fclose(fp);
}
void main()
{
int m,flag=1;
creat();
while(flag)
{
printf("-----------------------------------------\n");
printf("\t\t1-------添加\n");
printf("\t\t2-------查找\n");
printf("\t\t3-------修改\n");
printf("\t\t4-------删除\n");
printf("\t\t5-------输出\n");
printf("\t\t6-------退出\n");
printf("-----------------------------------------\n");
printf("\t请选择(1~~~6)\n");
scanf("%d",&m);
switch(m)
{
case 1: append(); break;
case 2: search(); break;
case 3: modify(); break;
case 4: delet(); break;
case 5: output(); break;
case 6: exit(0);
}
}
}
后面的修改、查找、删除、输出都运行不了,C++停止工作怎么回事?求教