读取数据出现乱码怎么回事
程序代码:
#include <stdio.h> #include <stdlib.h> #include <io.h> typedef struct { int eid; //员工号 char name[30]; //姓名 char gender[10]; //性别 int age; //年龄 double score; //综合得分 char level; //等级 }employee; typedef struct list { employee person; struct list* next; }list; //定义一个头结点 list *Init() { list *head; head=(list *)malloc(sizeof(list)); if(head==NULL) return NULL;//申请失败 head->next=NULL; return head; } //等级判断 void divide(list *head) { list *q; q=head; while(q!=NULL) { if((q->person.score)>=90) q->person.level='O'; else if((q->person.score)>=80) q->person.level='E'; else if((q->person.score)>=70) q->person.level='A'; else if((q->person.score)>=60) q->person.level='P'; else if(60>(q->person.score)) q->person.level='D'; q=q->next; } } //读取文件中的数据 void Load(list *head) { FILE *fp; list *p,*q; p=head; p->next=NULL; if(0==access("c:\\配置.ini",0)) { if((fp=fopen("c:\\配置.ini","rb"))==NULL) return; do { q=(list *)malloc(sizeof(list)); if(q!=NULL) { if(1==fread(q,sizeof(employee),1,fp)) { p->next=q; q->next=NULL; p=p->next; } else q=NULL; } } while(q); fclose(fp); } } //保存到文件中 void save(list *head) { FILE *fp; list *p; if((fp=fopen("c:\\配置.ini","wb"))==NULL) { printf("文件无法创建请重试!\n"); } p=head->next; while(p!=NULL) { if(fwrite(p,sizeof(list),1,fp)!=1) printf("文件写入错误!\n"); p=p->next; } printf("文件写入成功!\n"); fclose(fp); } //录入员工数据 void Input(list *head) { char ch; list *p,*q; p=head; while(p->next!=NULL) p=p->next; do { q=(list *)malloc(sizeof(list)); fflush(stdin); printf("请输入员工的信息。\n"); printf("工号:"); scanf("%d",&q->person.eid); printf("姓名:"); scanf("%s",q->person.name); printf("性别:"); scanf("%s",q->person.gender); printf("年龄:"); scanf("%d",&q->person.age); printf("综合得分:"); q->person.score=q->person.score; scanf("%lf",&q->person.score); q->next=NULL; p->next=q; p=p->next; divide(head); printf("录入联系人%s成功,是否继续录入?(Y/N) ",q->person.name); fflush(stdin); ch=getchar(); } while(ch!='N'&&ch!='n'); save(head); } //读出数据 void Display(list *head) { list *q; q=head->next; printf("\n-------------------------"); printf("\n工号\t姓名\t性别\t年龄\t综合得分\t等级"); printf("\n-------------------------"); while(q!=NULL) { printf("\n%d",q->person.eid); printf("\t%s",q->person.name); printf("\t%s",q->person.gender); printf("\t%d",q->person.age); printf("\t%0.1f",q->person.score); printf("\t%c",q->person.level); q=q->next;//指向下一员工 } printf("\n-------------------------"); printf("\n"); } //菜单 int menu() { char c; do{ system("cls"); printf("\t\t************** 员工管理系统 **************\n"); printf("\t\t 1.读取当前文件已有员工信息 \n"); printf("\t\t 2.录入员工信息 \n"); printf("\t\t 3.显示所有员工信息 \n"); printf("\t\t 4.查询员工信息 \n"); printf("\t\t 5.修改员工信息 \n"); printf("\t\t 6.统计员工信息 \n"); printf("\t\t 7.对员工信息进行排序 \n"); printf("\t\t 0.退出系统 \n"); printf("\t\t******************************************\n"); printf("\t\t请输入您的选择(0-7):"); fflush(stdin); c=getchar(); } while(c<'0'||c>'7'); return(c-'0'); } void main() { list *head; head=Init(); while(1) { switch(menu()) { case 0: printf("\t\t谢谢使用本程序,再见!\n"); printf("\t\t"); system("pause"); exit(0); case 1: Load(head); system("pause"); break;//读取当前文件已有员工信息 case 2:Input(head); system("pause"); break;//录入员工信息 case 3: Display(head); system("pause"); break; case 4:;system("pause"); case 5:;system("pause"); case 6:;system("pause"); case 7:;system("pause"); } } }
[此贴子已经被作者于2016-5-26 22:35编辑过]