在運行“添加”這一塊時,輸入完信息之後就提示***.EXE已停止工作,這是什麼錯?
代碼如下
#include
#include
#include
#include
#include
#define LENTEL sizeof(tel)
#define ID struct tel
struct tel
{
char name[15];
char tele[15];
char style[15];
char mail[20];
ID * next;
};
ID *id = NULL;
/*讀取telephone.txt文件中的內容創建學生鏈表*/
ID* create()
{
ID head;
ID *p1,*p2;
p2 = (ID)malloc(LENTEL);
head = p2;
FILE *fp;
if((fp = fopen("telephone.txt","r")) == NULL)
{
printf("聯系人文件打開錯誤,請將telephone.txt放置與程序同一目錄下!\n");
exit(0);
}
for(int i = 0; i<1;i++)
fscanf(fp,"%*[^\n]%*c");
while(!feof(fp))
{
p1 = (ID*)malloc(LENTEL);
fscanf(fp,"%s%s%s%s",p1->name,p1->tele,p1->style,p1->mail);
p1->next = NULL;
p2->next = p1;
p2 = p1;
}
fclose(fp);
return head;
}
ID* findbyname(char name[])
{
ID* p = id;
ID* temp = NULL;
while (p!=NULL)
{
if(strcmp(name,p->name) == 0)
{
temp = p;
break;
}
else
{
p = p->next;
}
}
return temp;
}
ID* findbytele(char tele[])
{
ID* p = id;
ID* temp = NULL;
while (p!=NULL)
{
if(strcmp(tele,p->tele) == 0)
{
temp = p;
break;
}
else
{
p = p->next;
}
}
return temp;
}
/*判斷這個聯系人是否存在*/
bool Istel(char name[])
{
ID *p = id->next;
bool flag = false;
while (p != NULL)
{
if(strcmp(p->name,name) == 0)
{
flag = true;
}
p=p->next;
}
return flag;
}
int add()
{
ID *newtel = NULL;
ID *p = id;
char tel_name[15];
char tel_tele[11];
char tel_style[15];
char tel_mail[20];
printf("\n------- 請輸入學生基本信息 --------\n");
printf("姓名 電話 類別 郵箱\n");
printf("--------------------------------------\n");
scanf("%s%s%s%s",tel_name,tel_tele,tel_style,tel_mail);
if(!Istel(tel_name))
{
newtel = (ID*)malloc(LENTEL);
strcpy(newtel->name,tel_name);
strcpy(newtel->tele,tel_tele);
strcpy(newtel->style,tel_style);
strcpy(newtel->mail,tel_mail);
newtel->next = NULL;
for(p=p->next; p->next!=NULL; p=p->next);
p->next = newtel;
printf("\n聯系人已添加成功!\n");
}
else
{
printf("\n該聯系人姓名已存在!\n");
}
getch();
return 0;
}
void Print_Inquire_All()
{
void menu_print_out();
ID *p1 = id->next;
menu_print_out();
while (p1!=NULL)
{
printf("%s\t%s\t%s\t%s\n",p1->name,p1->tele,p1->style,p1->mail);
p1 = p1->next;
}
system("pause");
}
void menu_print_out()
{
printf("\n---------------------------------\n");
printf("\n---------------------------------\n");
}
/*主菜單*/
void menu(void)
{
void ReadBack();
printf(" ╭════════╮ \n");
printf("╭══════╣通訊管理系統V1.0╠══════╮\n");
printf("║ ╰════════╯ ║\n");
printf("║ 【1】添加數據 【3】退出系統 ║\n");
printf("║ ║\n");
printf("║ 【2】查詢數據 ************* ║\n");
printf("║ ║\n");
printf("╰══════════════════════╯\n");
printf("◎請輸入功能前的序號進入相應的工具:【 】\b\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3')
{
printf("error! please input the right number!\n");
putchar('\a');
getchar();
printf("◎請重新輸入功能前的序號進入相應的工具:【 】\b\b");
a = getchar();
}
switch(a)
{
case '1':
add();
break;
case '2':
Print_Inquire_All();
case '3':
ReadBack();
exit(0);
break;
}
getchar();
}
/*退出程序時將數據寫回telephone.txt進行更新*/
void ReadBack()
{
FILE *TEL = fopen("telephone.txt","w");
ID *te = id->next;
fprintf(TEL,"Tname Ttele Tstyle Tmail");
while (te != NULL)
{
fprintf(TEL,"\n%s %s %s %s",te->name,te->tele,te->style,te->mail);
te = te->next;
}
fclose(TEL);
}
void Lunch()
{
FILE *fp;
if((fp=fopen("telephone.txt","r")) == NULL)
{
FILE *fp;
fp=fopen("telephone.txt","w+");
}
else
id = create();
menu();
}
void main()
{
SetConsoleTitle("C語言通訊錄管理系統");
Lunch();
}
第一,輸入的字符串長度沒有超過定義吧!其次,分配的指針在使用前要判斷是否為空。最後,單步調試,、或增加打印看看具體是哪行出錯了。