编写信息管理系统-系统时间添加的问题
仿一位前辈的电话簿程序~在准备增加录入时间的显示及存储时出现了些许问题~语法上貌似没有错误~但是运行时有错~
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#include <winbase.h>
#include<time.h>
typedef struct node{
char name[20];
char address[40];
char phone[15];
char time[128];
struct node *next;
}add_list;
struct person{
char name[20];
char address[40];
char phone[15];
char time[128];
};
FILE *fp;
char filename[]="phonebook.txt";
char c;
add_list *head,*tail;
add_list *load(char filename[]) //从文件载入通讯录
{
add_list *new1,*head;
struct person t;
head=(add_list *)malloc(sizeof(add_list));
tail=head=NULL;
if((fp=fopen(filename,"rb"))==NULL)
return head;
else
if(!feof(fp))//文件未结束输出0 继续往下
if(fread(&t,sizeof(struct person),1,fp)==1)//从文件指针fp读取一个size为sizeof(struct person)的数据,存入t,if用来判断读取是否成功
{
new1=(add_list *)malloc(sizeof(add_list));//连入链表第一个节点
strcpy(new1->name,t.name);
strcpy(new1->address,t.address);
strcpy(new1->phone,t.phone);
strcpy(new1->time,t.time);
head=tail=new1;
while(!feof(fp))//连入其余节点
{
if(fread(&t,sizeof(struct person),1,fp)==1)
{
new1=(add_list *)malloc(sizeof(add_list));
strcpy(new1->name,t.name);
strcpy(new1->address,t.address);
strcpy(new1->phone,t.phone);
strcpy(new1->time,t.time);
tail->next=new1;
new1->next=NULL;
tail=new1;
}
}
}
fclose(fp);
return head;
}
void insert(add_list **head)//指向指针的指针,也就是存放一个变量的地址的地址
{
add_list *new1;
char ch[128];
time_t t;
memset(ch,0x0,128);
time(&t);
sprintf(ch,"%s", ctime(&t));
new1=(add_list *)malloc(sizeof(add_list));
printf("please input the name:");getchar();gets(new1->name);
printf("please input the address:");scanf("%s",new1->address);
printf("please input the phone number:");scanf("%s",new1->phone);
strcpy(ch,new1->time);
if(*head==NULL)
{
*head=new1;
new1->next=NULL;
tail=new1;
}
else
{
tail->next=new1;
new1->next=NULL;
tail=new1;
}
getchar();
}
void save(add_list *head,char filename[])
{
add_list *p;
struct person t;
if((fp==fopen(filename,"wb"))==NULL)
{
printf("ERROR:cannot open file %s\n",filename);
getchar();
}
else
{
p=head;
while(p!=NULL)
{
strcpy(t.name,p->name);
strcpy(t.address,p->address);
strcpy(t.phone,p->phone);
strcpy(t.time,p->time);
fwrite(&t,sizeof(struct person),1,fp);//将t指针指向的内容,写入到fp流中
p=p->next;
}
}
fclose(fp);
}
void display(add_list *head)
{
add_list *p;
p=head;
if(p!=NULL)
printf("name:\t\taddress:\t\tphone number:\t\ttime:\n");
while(p!=NULL)
{
printf("%s\t\t%s\t\t%s\t\t%s\n",p->name,p->address,p->phone,p->time);
p=p->next;
}
getchar();
}
int menu()
{
while(1)
{
printf("=== 1add a new contact ===\n");
printf("=== 2delete a contact ===\n");
printf("=== 3search a contact ===\n");
printf("=== 4view the contacts ===\n");
printf("=== 5save the contacts ===\n");
printf("=== 6exit ===\n");
printf("=====================================================\n");
printf("please select a number and then input enter:");
c=getchar();
switch(c)
{
case'1':
insert(&head);
printf("please input enter and go ahead");
getchar();
system("cls");
break;
case'4':
display(head);
printf("please input enter and go ahead");
getchar();
system("cls");
break;
case'5':
save(head,filename);
printf("saved,please input enter and go ahead");
getchar();
getchar();
system("cls");
break;
}
}
}
void main()
{
while(1)
{
head=load(filename);
menu();
}
}
问题貌似主要是74行那块~void insert(add_list **head)函数里边//指向指针的指针,也就是存放一个变量的地址的地址
strcpy(ch,new1->time);
if(*head==NULL)
{
*head=new1;
new1->next=NULL;
tail=new1;
}
各位前辈~求解一下原因~调试时ch已经存入时间的字符串了,但是new1->time 存入的确实“坉坉坉坉坉。。。。”~~先行谢过
顺便再问多个问题~要是要对存入信息排序按名字首字母排的话~各位会怎么编写~?









代码太长了吧........先留着,晚上看


明天给你答复吧,对不住
~~keep moving~