c语言 用链表读取文件,进行操作,能运行了,可是各项功能无法实现,输出还乱码,
# include <stdio.h># include <stdlib.h>
# include <malloc.h>
# include <string.h>
# define LEN sizeof(struct Medical)
struct Medical{
int num;
char siteid[10];
char disease[10];
char office[10];
struct Medical * next;
};
struct Medical * creat(){
struct Medical * head;
struct Medical * p1,* p2;
head=NULL;
FILE * fp;
if ((fp=fopen("G:\\medical.txt","r"))==NULL){
printf("cannot open file\n");
exit(0);
}
head=(struct Medical *)malloc(LEN);
memset(head,0,LEN); //防止字符乱码,不知道对不对
head->next=NULL;
p2=head;
while (!feof(fp)){
p1=(struct Medical *)malloc(LEN);
memset(p1,0,LEN); //防止字符乱码
p1->next=NULL;
fread(p2,LEN,1,fp);
p2->next=p1;
p2=p1;
}
if (feof(fp)){
p2->next=NULL;
return (head);
}
return (head);
}
struct Medical * insert(struct Medical* head,struct Medical * med){
struct Medical * p0,* p1,* p2;
p1=head;
p0 =med;
while ((p0->num > p1->num)&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
}
p2->next=p0;
if(p0->num <= p1->num){
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else{
p1->next=p0;
p0->next=NULL;
}
return(head);
}
struct Medical * delet (struct Medical * head,int number ){
struct Medical * p1,* p2;
p1=head;
while (number!=p1->num && p1->next!=NULL){
p2=p1;
p1=p1->next;
}
if(number == p1->num){
if(p1 == head)
head=p1->next;
else
p2->next=p1->next;
printf("已删除\n");
}
else
printf("没有发现要删除的项目\n");
return (head);
}
void print(struct Medical * head){
struct Medical * p;
printf("全部信息为:\n");
p=head;
do{
printf(" %d %s %s %s",p->num,p->siteid,p->disease,p->office);
p=p->next;
}while(p!=NULL);
}
void sort(){
struct Medical * p1,* p2,* p0;
struct Medical * head;
for(p1=head;p1!=NULL;p1->next){
for(p2=p1->next;p2!=NULL;p2->next){
if(p1->siteid>p2->siteid){
p0=p1;
p1=p2;
p2=p0;
}
}
}
}
void find(){
struct Medical * head;
struct Medical * p;
char id[10]={0},dise[10]={0},offi[10]={0};
int n,number;
printf("1、按siteid查找\n2、按疾病名称查找\n3、按科室查找\n4、按序号查找\n请选择\n");
scanf("%d",&n);
if(n==1){
printf("请输入siteid\n");
scanf("%s",id);
for(p=head;p!=NULL;p->next){
if(strcmp(id,p->siteid)==0)
printf("%d %s %s %s",p->num,p->siteid,p->disease,p->office);
else
printf("无法找到信息\n");
}
}
else if(n==2){
printf("请输入disease\n");
scanf("%s",dise);
for(p=head;p!=NULL;p->next){
if(strcmp(dise,p->disease)==0)
printf("%d %s %s %s",p->num,p->siteid,p->disease,p->office);
else
printf("无法找到信息\n");
}
}
else if(n==4){
printf("请输入number\n");
scanf("%d",&number);
for(p=head;p!=NULL;p=p->next){
if(number==p->num)
printf("%d%s%s%s",p->num,p->siteid,p->disease,p->office);
else
printf("无法找到信息\n");
}
}
else {
printf("请输入office\n");
scanf("%s",offi);
for(p=head;p!=NULL;p->next){
while(strcmp(offi,p->office)==0){
printf("%d %s %s %s",p->num,p->siteid,p->disease,p->office);
}
}
}
}
int main (){
struct Medical * creat();
struct Medical*insert(struct Medical *,struct Medical *);
struct Medical* delet(struct Medical *,int );
void print(struct Medical* head);
void sort();
void find();
int i,number;
struct Medical med;
struct Medical * head;
char id[10]={0};
FILE * fp;
if ((fp=fopen("G:\\medical.txt","r"))==NULL){
printf("cannot open file\n");
exit(0);
}
head=creat();
printf(" 1、查找项目\n 2、项目排序\n 3、删除项目\n 4、添加项目\n 5、退出\n");
scanf("%d",&i);
switch(i){
case 1:
find();
break;
case 2:
sort();
print(head);
break;
case 3:
printf("请输入要删除的项目序号\n");
scanf("%d",&number);
head=delet(head,number);
print(head);
break;
case 4:
printf("请输入要插入的项目\n");
scanf("%d %s %s %s",&med.num,med.siteid,med.disease,med.office);
head=insert(head,&med);
print(head);
break;
case 5:
exit(0);
break;
}
fclose(fp);
return 0;
}