求助~数据块文件读写之后变成乱码怎么办
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct patient
{
char id[20];
char name[20];
char sex[5];
int age,year,month,day;
char cases[200];
char consumption[20];
struct patient *next;
}PATIENT;
PATIENT * Input_Patient();
void Save_Patient(PATIENT *head);
void Find_Patient();
int main()
{
PATIENT *h;
h=Input_Patient();
Find_Patient();
return 0;
}
PATIENT * Input_Patient()
{
PATIENT *p1,*p2,*head;
int flag=1,x;
head=(PATIENT *)malloc(sizeof(PATIENT));
p1=head;
while(flag==1)
{
p2=(PATIENT *)malloc(sizeof(PATIENT));
printf("请输入病人ID:");
scanf("%s",p1->id);
printf("请输入病人姓名:");
scanf("%s",p1->name);
printf("请输入病人性别:");
scanf("%s",p1->sex);
printf("请输入病人年龄:");
scanf("%d",&p1->age);
printf("请输入病人入院时间(年/月/日):");
scanf("%d%d%d",&p1->year,&p1->month,&p1->day);
printf("请输入病人病例:");
scanf("%s",p1->cases);
printf("请输入病人消费情况:");
scanf("%s",p1->consumption);
p1->next=p2;
p1=p2;
printf("继续输入病人信息请按 1:");
scanf("%d",&flag);
}
p2->next=NULL;
printf("病人信息输入结束!\n是否保存病人信息?(1.是/2.否)\n");
scanf("%d",&x);
if(x==1)
Save_Patient(head);
///else
///Main_Patient();
return head;
}
void Save_Patient(PATIENT *head)
{
PATIENT *p;
FILE *fp;
if((fp=fopen("patient","a"))==NULL)
{
printf("打开文件过程出现错误!\n");
///Main_Patient();
}
for(p=head->next;p!=NULL;p=p->next)
fwrite(p,sizeof(PATIENT),1,fp);
fclose(fp);
printf("病人信息已存入!\n");
}
void Find_Patient()
{
PATIENT *p;
FILE *fp;
int x;
char id[20],name[20];
p=(PATIENT *)malloc(sizeof(PATIENT));
printf("请选择你要使用的查找方式:\n");
printf("1.通过ID查找\n");
printf("2.通过姓名查找\n");
scanf("%d",&x);
if(x==1)
{
printf("\n请输入病人ID:");
scanf("%s",id);
if((fp=fopen("patient","r"))==NULL)
{
printf("打开文件过程中出现错误!\n");
exit(0);
}
while(!feof(fp))
{
fread(p,sizeof(PATIENT),1,fp);
if(strcmp(p->name,name)==0)
{
printf("ID:%s\n姓名:%s\n性别:%s\n年龄:%d\n",p->id,p->name,p->sex,p->age);
printf("入院时间:%02d/%02d/%02d\n病例:%s\n消费情况:%s\n",p->year,p->month,p->day,p->cases,p->consumption);
}
}
fclose(fp);
}
else if(x==2)
{
printf("\n请输入病人姓名:");
scanf("%s",name);
if((fp=fopen("patient","r"))==NULL)
{
printf("打开文件过程中出现错误!\n");
exit(0);
}
while(!feof(fp))
{
fread(p,sizeof(PATIENT),1,fp);
if(strcmp(p->name,name)==0)
{
printf("ID:%s\n姓名:%s\n性别:%s\n年龄:%d\n",p->id,p->name,p->sex,p->age);
printf("入院时间:%02d/%02d/%02d\n病例:%s\n消费情况:%s\n",p->year,p->month,p->day,p->cases,p->consumption);
}
}
fclose(fp);
}
}
[此贴子已经被作者于2018-3-12 20:55编辑过]