|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- #define LEN sizeof(struct Student)
- union bumen
- {
- int banji;
- char zhiwu[20];
- };
- struct Student
- {
-
- int inumber;
- char iname[20];
- char isex[10];
- char job;
- union bumen aa;
- struct Student *next;
- };
- int n;
- struct Student *create()
- {
- struct Student *p1,*p2,*phead;
- p1=p2=(struct Student*)malloc(LEN);
- printf("请输入学生学号");
- scanf("%d",&p1->inumber);
- printf("请输入学生姓名");
- scanf("%s",&p1->iname);
- printf("请输入性别");
- scanf("%s",&p1->isex);
- fflush(stdin);
- printf("请输入工作");
- scanf("%c",&p1->job);
- if(p1->job=='s')
- {
- printf("请输入班级");
- scanf("%d",&p1->aa.banji);
-
- }
- else
- {
- if(p1->job=='t')
- {
- printf("请输入职务");
- scanf("%s",p1->aa.zhiwu);
- }
- else
- {
- printf("ERROR!");
- }
- }
- phead=NULL;
- n=0;
- while(p1->inumber)
- {
- n++;
- if(n==1)
- {
- phead=p1;
- }
- else
- {
- p2->next=p1;
- }
- p2=p1;
- p1=(struct Student*)malloc(LEN);
- printf("请输入学生学号");
- scanf("%d",&p1->inumber);
- printf("请输入学生姓名");
- scanf("%s",&p1->iname);
- printf("请输入性别");
- scanf("%s",&p1->isex);
- fflush(stdin);
- printf("请输入工作");
- scanf("%c",&p1->job);
- if(p1->job=='s')
- {
- printf("请输入班级");
- scanf("%d",&p1->aa.banji);
-
- }
- else
- {
- if(p1->job=='t')
- {
- printf("请输入职务");
- scanf("%s",p1->aa.zhiwu);
- }
- else
- {
- printf("ERROR!");
- }
- }
- p2->next=NULL;
- }
- return phead;
- }
- void save(struct Student *p)
- {
- FILE *fp;
- char filename[10];
- printf("请输入要创建的文件名");
- scanf("%s",filename);
- if(!(fp=fopen(filename,"wt+")))
- {
- printf("打开文件错误");
- exit(0);
- }
- while(p->inumber) //调试中这一步出错,但是不知道为什么
- {
- if(fwrite(p,sizeof(struct Student),1,fp)!=1)
- {
- printf("输入错误");
- return;
- }
- else
- {
- fwrite(p,sizeof(struct Student),1,fp);
-
- }
- p=p->next;
- }
- fclose(fp);
- }
- void main()
- {
- struct Student *str;
- str=create();
- save(str);
- }
复制代码 |
|