Linux下学生信息管理系统(共35页).doc

上传人:飞****2 文档编号:12173907 上传时间:2022-04-23 格式:DOC 页数:35 大小:139KB
返回 下载 相关 举报
Linux下学生信息管理系统(共35页).doc_第1页
第1页 / 共35页
Linux下学生信息管理系统(共35页).doc_第2页
第2页 / 共35页
点击查看更多>>
资源描述

《Linux下学生信息管理系统(共35页).doc》由会员分享,可在线阅读,更多相关《Linux下学生信息管理系统(共35页).doc(35页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进行操作(插入、修改和删除)的C程序,学生基本信息以结构体的形式存储在文件专心-专注-专业#include#include#include#include#includestruct stu_info char stu_num12;char name10;char sex;/*0为女生,1为男生*/char mobile_phone12;struct stu_info student;unsigned int number;struct stu_info stu100;FILE *in;FI

2、LE *out;int len;void inputstu();void updatestu();void deletestu();void displaystu();void commanddisp()printf(-n); printf(|-学生基本信息管理-|n);printf(|-1.基本信息插入.-2.基本信息修改-|n);printf(|-3.基本信息删除.-4.基本信息显示-|n);printf(|-r.初始化(第一次使用时用!慎用)-q.退出-|n);printf(|-请输入你的选择:-|n);printf(-n);void main()commanddisp();char c

3、h;while(1)doch=getchar();while(ch=n);if(ch=q)return;switch(ch)case 1:inputstu(); break;case 2:updatestu();break;case 3:deletestu();break;case 4:displaystu();break;case r:printf(ninit student tablen);if(out=fopen(c.doc,w)=NULL) return;number=0;fwrite(&number,sizeof(unsigned int),1,out);fflush(out);fc

4、lose(out);break;default:break;printf(please enter any key to continue);getchar();getchar();system(clear);commanddisp();void inputstu()printf(n-请输入学生信息-n);char p=y;while(p=y|p=Y)memset(&student,0,sizeof(student);printf(学号:);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(性别0是女,1是男

5、:);dostudent.sex=getchar();while(student.sex=n);printf(手机号:);scanf(%s,student.mobile_phone);len=sizeof(struct stu_info);if(out=fopen(c.doc,r+)=NULL) return;fread(&number,sizeof(unsigned int),1,out);number+;fseek(out,0L,SEEK_SET);fwrite(&number,sizeof(unsigned int),1,out);fflush(out);fseek(out,0L,SEE

6、K_END);fwrite(&student,len,1,out);fflush(out);fclose(out);printf(是否继续输入YNn);fflush(stdin);dop=getchar();while(p=n);void updatestu()printf(n-学生信息修改-n);printf(请输入修改学生的学号:);memset(&student,0,sizeof(student);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(性别0是女,1是男:);dostudent.sex=ge

7、tchar();while(student.sex=n);printf(手机号:);scanf(%s,student.mobile_phone);len=sizeof(struct stu_info);in=fopen(c.doc,r+);fread(&number,sizeof(unsigned int),1,in);unsigned int j;long kk;for(j=0;jnumber;j+)fread(&stu0,len,1,in);if(strcmp(student.stu_num,stu0.stu_num)=0)kk=len;fseek(in,-kk,SEEK_CUR);fwr

8、ite(&student,len,1,in);break;fflush(in);fclose(in);if(j=number)printf(没有这条记录n);void deletestu()printf(n-学生信息删除-n);in=fopen(c.doc,r);fread(&number,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int i;char f;while(1)printf(请输入删除学生的学号:)

9、;char stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);scanf(%s,stu_num);for(i=0;inumber;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number) printf(没有这条记录n);else number-;for(;inumber;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(stui.name,stui+1.name);stui.sex=stui+1.sex;strcpy(stui.mobi

10、le_phone,stui+1.mobile_phone);printf(是否继续删除YNn);fflush(stdin);dof=getchar();while(f=n);if(f!=y&f!=Y)break;fclose(in);out = fopen(c.doc,w);fwrite(&number,sizeof(unsigned int),1,out);len=len*number;fwrite(stu,len,1,out);fflush(out);fclose(out);void displaystu()printf(n-显示学生信息-n);in=fopen(c.doc,r);frea

11、d(&number,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int i;printf(-n);printf(number t学号t 姓名t性别 手机号n);for(i=0;inumber;i+)printf( %3d %12s%10s %c %12sn,i,stui.stu_num,stui.name,stui.sex,stui.mobile_phone);printf(-n);fclose(in); 利用父

12、子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其中父进程接受用户对文件stu.info的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作。#include#include#include#include#includestruct stu_info char stu_num12;char name10; char sex;/*0为女生,1为男生*/char mobile_phone12;struct stu_info student;unsigned int number;struct stu_info stu100;int pipefiles2;void m

13、ain() printf(-n); printf(|-学生基本信息管理-|n); printf(|-1.基本信息插入.-2.基本信息修改-|n); printf(|-3.基本信息删除.-4.基本信息显示-|n); printf(|-r.初始化(第一次使用时用!慎用)-|n); printf(|-请输入你的选择:-|n:); printf(-n); pid_t pid;FILE *in;FILE *out;char ch;int len; if(pipe(pipefiles)=0) pid=fork(); if(pid=-1) fprintf(stderr,Fork failuren);retu

14、rn; if(pid=0)/child.read(pipefiles0,&ch,1);switch(ch)case 1:while(1)sleep(1);len=sizeof(struct stu_info);char *ch1=(char*)malloc(sizeof(char)*len);read(pipefiles0,ch1,len);if(out=fopen(c.doc,r+)=NULL) return;fread(&number,sizeof(unsigned int),1,out);number+;fseek(out,0L,SEEK_SET);fwrite(&number,size

15、of(unsigned int),1,out);fclose(out);if(out=fopen(c.doc,a)=NULL) return;fwrite(ch1,len,1,out);fclose(out);char f;read(pipefiles0,&f,sizeof(char);if(f!=y&f!=Y) return;break;case 2:sleep(1);len=sizeof(struct stu_info);read(pipefiles0,&student,len);in=fopen(c.doc,r);fread(&number,sizeof(unsigned int),1,

16、in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int j;for(j=0;jnumber;j+)if(strcmp(student.stu_num,stuj.stu_num)=0)strcpy(stuj.name,student.name);stuj.sex=student.sex;strcpy(stuj.mobile_phone,student.mobile_phone);break;fclose(in);if(j=number)printf(没有这条记录n

17、);elseout = fopen(c.doc,w);fwrite(&number,sizeof(unsigned int),1,out);len=sizeof(struct stu_info);len=len*number;fwrite(&stu,len,1,out);fclose(out);break;case 3:while(1)sleep(1);char stu_num12;memset(stu_num,0,sizeof(stu_num);read(pipefiles0,stu_num,sizeof(stu_num);in=fopen(c.doc,r);fread(&number,si

18、zeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int i;for(i=0;inumber;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number) printf(没有这条记录n);else for(;inumber-1;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(stui.name,stui+1.name);stui.

19、sex=stui+1.sex;strcpy(stui.mobile_phone,stui+1.mobile_phone);if(out=fopen(c.doc,r)=NULL) return;fread(&number,sizeof(unsigned int),1,out);fclose(out);number-;out = fopen(c.doc,w);fwrite(&number,sizeof(unsigned int),1,out);len=sizeof(struct stu_info);len=len*number;fwrite(stu,len,1,out);fclose(out);c

20、har f;read(pipefiles0,&f,sizeof(int);if(f!=y&f!=Y) return;break;case 4:sleep(1);in=fopen(c.doc,r);fread(&number,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int i;printf(-n);for(i=0;inumber;i+)printf(-%d-学号: %s-姓名: %s-性别: %c-手机号: %

21、s-n,i,stui.stu_num,stui.name,stui.sex,stui.mobile_phone);printf(-n);fclose(in); break;case r:if(out=fopen(c.doc,w)=NULL) return;number=0;fwrite(&number,sizeof(unsigned int),1,out);break;default :break;else/father.doch=getchar();while(ch=n);write(pipefiles1,&ch,1);switch(ch)case 1:printf(n-请输入学生信息-n)

22、;char p=y;while(p=y|p=Y)memset(&student,0,sizeof(student);printf(学号:);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(性别0是女,1是男:);dostudent.sex=getchar();while(student.sex=n);printf(手机号:);scanf(%s,student.mobile_phone);len=sizeof(struct stu_info);write(pipefiles1,&student,len);sl

23、eep(1);printf(是否继续输入YNn);fflush(stdin);dop=getchar();while(p=n);write(pipefiles1,&p,sizeof(char); break;case 2:printf(n-学生信息修改-n);printf(请输入修改学生的学号:);memset(&student,0,sizeof(student);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(性别0是女,1是男:);dostudent.sex=getchar();while(studen

24、t.sex=n);printf(手机号:);scanf(%s,student.mobile_phone);len=sizeof(struct stu_info);write(pipefiles1,&student,len);sleep(2);break;case 3:printf(n-学生信息删除-n);while(1)printf(请输入删除学生的学号:);char stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);scanf(%s,stu_num);write(pipefiles1,stu_num,sizeof(stu_num

25、);sleep(1);printf(是否继续删除YNn);fflush(stdin);char f;dof=getchar();while(f=n);write(pipefiles1,&f,sizeof(int);if(f!=y&f!=Y)return;break;case 4:printf(n-显示学生信息-n);sleep(1);break;case r:printf(ninit student tablen);sleep(2);break;default:break; 学习并使用线程间的同步方法,重新改写实验3步骤6要求的程序。要求启用两个线程,其中主线程接受用户对文件stu.info的

26、操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)。#include#include#include#include#include#include#include struct stu_info char stu_num12;char name10;char sex;/*0为女生,1为男生*/char mobile_phone12;struct stu_info student;unsigned int number;struct stu_info stu100;FILE *in;FILE *out;int len;sem_t bin_sem;sem_t bin_sem1

27、;char ch;void inputstu();void updatestu();void deletestu();void displaystu();void *stuthread(void *com);void commanddisp()printf(-n); printf(|-学生基本信息管理-|n);printf(|-1.基本信息插入.-2.基本信息修改-|n);printf(|-3.基本信息删除.-4.基本信息显示-|n);printf(|-r.初始化(第一次使用时用!慎用)-q.退出-|n);printf(|-请输入你的选择:-|n);printf(-n);void main()

28、commanddisp();int res;pthread_t a_thread;void *thread_result;res = sem_init(&bin_sem,0,0);if (res != 0)perror(Semaphore initialition failed);exit(EXIT_FAILURE);res = sem_init(&bin_sem1,0,0);if (res != 0)perror(Semaphore initialition failed);exit(EXIT_FAILURE);res = pthread_create(&a_thread,NULL,stut

29、hread,NULL);if(res != 0)perror(Thread creation failed);exit(EXIT_FAILURE);while(1)doch=getchar();while(ch=n);if(ch=q)sem_post(&bin_sem);break;sem_post(&bin_sem);sem_wait(&bin_sem1);printf(please enter any key to continue);getchar();getchar();system(clear);commanddisp();res = pthread_join(a_thread,&t

30、hread_result);if(res != 0)perror(Thread join failed);exit(EXIT_FAILURE);sem_destroy(&bin_sem);sem_destroy(&bin_sem1);exit(EXIT_SUCCESS);void *stuthread(void *com)sem_wait(&bin_sem);while(ch!=q)switch(ch)case 1:inputstu(); break;case 2:updatestu();break;case 3:deletestu();break;case 4:displaystu();br

31、eak;case r:printf(ninit student tablen);if(out=fopen(c.doc,w)=NULL) exit(EXIT_FAILURE);number=0;fwrite(&number,sizeof(unsigned int),1,out);fflush(out);fclose(out);break;default:break;sem_post(&bin_sem1);sem_wait(&bin_sem);pthread_exit(NULL);void inputstu()printf(n-请输入学生信息-n);char p=y;while(p=y|p=Y)m

32、emset(&student,0,sizeof(student);printf(学号:);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(性别0是女,1是男:);dostudent.sex=getchar();while(student.sex=n);printf(手机号:);scanf(%s,student.mobile_phone);len=sizeof(struct stu_info);if(out=fopen(c.doc,r+)=NULL) return;fread(&number,sizeof(u

33、nsigned int),1,out);number+;fseek(out,0L,SEEK_SET);fwrite(&number,sizeof(unsigned int),1,out);fflush(out);fseek(out,0L,SEEK_END);fwrite(&student,len,1,out);fflush(out);fclose(out);printf(是否继续输入YNn);fflush(stdin);dop=getchar();while(p=n);void updatestu()printf(n-学生信息修改-n);printf(请输入修改学生的学号:);memset(&student,0,sizeof(student);scanf(%s,student.stu_num);printf(姓名:);scanf(%s,student.name);printf(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁