在线求助 ,,C语言改错,,,,,十分感谢
#include<stdio.h>#include<string.h>
#include<conio.h>
#include<stdlib.h>
typedef struct message //信息结构体
{
char TX_name[20];
char TX_sex[5];
char TX_phonenumber[12];
char TX_email[20];
struct message *next;
}Node;
Node *p;
void operate_menu(Node *head); //操作选项
void add_message(Node *head); //添加信息
void show_message(Node *head); //显示信息
void save_message(Node *head); //保存信息
void show_title(int date); //打印横线
Node *read_message() //从文件里读取信息到一个链表 可能就是这个函数有问题
void save_message(Node *head)
{
FILE *fp;
fp=fopen("date","wb");
if(fp==NULL)
{
printf("不能打开文件...\n");
exit(0);
}
p=head->next;
if(p!=NULL)
{
do
{
fprintf(fp,"%s\n",p->TX_name);
fprintf(fp,"%s\n",p->TX_sex);
fprintf(fp,"%s\n",p->TX_phonenumber);
fprintf(fp,"%s\n",p->TX_email);
p=p->next;
}while(p!=NULL);
}
fclose(fp);
}
void show_message(Node *head)
{
p=head->next;
printf("\n");
printf(" 姓名 性别 电话 邮箱 \n");
printf("==========================================================================\n");
while(p!=NULL)
{
printf(" %-12s",p->TX_name);
printf("\t %-8s",p->TX_sex);
printf("\t %-15s",p->TX_phonenumber);
printf(" %-25s\n",p->TX_email);
p=p->next;
}
printf("==========================================================================\n");
}
////////////////////////////////////////////////////////////////////////////
void add_message(Node *head) //lixiaoguang
{
Node *node=NULL;
p=head->next;
char *p1=NULL;
//system("cls");
char c;
do{
node=(Node *)malloc(sizeof(Node));
printf("请输入姓名:");
scanf("%s",node->TX_name);
p1=node->TX_name;
printf("请输入性别:");
scanf("%s",node->TX_sex);
printf("请输入电话:");
scanf("%s",node->TX_phonenumber);
printf("请输入邮箱:");
scanf("%s",node->TX_email);
if(p==NULL)
{
node->next=NULL;
head->next=node;
p=node;
}
else
{
node->next=head->next;
head->next=node;
p=node;
}
getchar();
printf("继续请按Y或y\n");
scanf("%c",&c);
}while(c=='Y'||c=='y');
printf("添加完毕\n");
}
void show_title(int date) ///////////////////////
{
int i=0;
printf("\n");
for(i;i<50;i++)
{
switch(date)
{
case 1: printf("E"); break;
case 2: printf("*"); break;
case 3: printf("#"); break;
case 4: printf("~"); break;
case 5: printf("T"); break;
default: printf("X");
}
}
printf("\n");
}
Node *read_message() /////////////////////// 可能就是这个函数有问题
{
Node *head=NULL;
FILE *fp;
Node *node=NULL;
head=(Node *)malloc(sizeof(Node));
head->next=NULL;
p=head;
fp=fopen("date.txt","ab+");
if(fp==NULL)
{
printf("打开文件失败....\n");
sleep(10000);
exit(0);
}
while(!feof(fp))
{
node=(Node *)malloc(sizeof(Node));
if(node==NULL)
{
exit(0);
}
if(fread(node,sizeof(Node),1,fp)==1)
{
node->next=NULL;
p->next=node;
p=node;
}
else //加载信息的时候直接走到这一步了,,没有走上面的读取信息
{
printf("000000000000000000000\n");
}
}
fclose(fp);
return head;
}
void operate_menu(Node *head) ///////////////////////
{
int choice;
show_title(1);
printf("\n\n");
system("color 5C");
printf("\t\t1. 添加信息\n");
printf("\t\t2. 显示信息\n");
printf("\t\t3. 保存信息\n");
printf("\n\n");
show_title(1);
printf("请选择(1-3):");
scanf("%d",&choice);
switch(choice)
{
case 1: add_message(head);
operate_menu(head);
break;
case 2: show_message(head);
operate_menu(head);
break;
case 3: save_message(head);
operate_menu(head);
break;
printf("\n\n");
show_title(1);
}
}
int main()
{
Node *head=NULL;
head=(Node *)malloc(sizeof(Node));
head=read_message();
operate_menu(head);
sleep(5000000);
return 0;
}