Linux下学生信息管理系统_第1页
Linux下学生信息管理系统_第2页
Linux下学生信息管理系统_第3页
Linux下学生信息管理系统_第4页
Linux下学生信息管理系统_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进行操作(插入、修改和删除)的C程序,学生基本信息以结构体的形式存储在文件#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>struct stu_info char stu_num12;char name10;char sex;/*0为女生,1为男生*/char mobile_phone12;struct stu_info studen

2、t;unsigned int number;struct stu_info stu100;FILE *in;FILE *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&quo

3、t;);printf("|-r.初始化(第一次使用时用!慎用)-q.退出-|n");printf("|-请输入你的选择:-|n");printf("-n");void main()commanddisp();char ch;while(1)doch=getchar();while(ch='n');if(ch='q')return;switch(ch)case '1':inputstu(); break;case '2':updatestu();break;case 

4、9;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);fclose(out);break;default:break;printf("please

5、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(&

6、quot;姓名:");scanf("%s",);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(&

7、amp;number,sizeof(unsigned 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

8、()printf("n-学生信息修改-n");printf("请输入修改学生的学号:");memset(&student,0,sizeof(student);scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性别0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手

9、机号:");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;j<number;j+)fread(&stu0,len,1,in);if(strcmp(student.stu_num,stu0.stu_num)=0)kk=len;fseek(in,

10、-kk,SEEK_CUR);fwrite(&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);f

11、read(stu,len,number,in);unsigned int i;char f;while(1)printf("请输入删除学生的学号:");char stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number) printf("没有这条记录n");else number-;for(;

12、i<number;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(,stui+1.name);stui.sex=stui+1.sex;strcpy(stui.mobile_phone,stui+1.mobile_phone);printf("是否继续删除YNn");fflush(stdin);dof=getchar();while(f='n');if(f!='y'&&f!='Y')break;fclose(in);out = fopen(&qu

13、ot;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");fread(&number,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);le

14、n=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int i;printf("-n");printf("number t学号t 姓名t性别<0是女,1是男> 手机号n");for(i=0;i<number;i+)printf(" %3d %12s%10s %c %12sn",i,stui.stu_num,,stui.sex,stui.mobile_phone);printf("-n");fclose(in);

15、利用父子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其中父进程接受用户对文件的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作。#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>struct stu_info char stu_num12;char name10; char sex;/*0为女生,1为男生*/char mobile_phone12;struct s

16、tu_info student;unsigned int number;struct stu_info stu100;int pipefiles2;void main() printf("-n"); printf("|-学生基本信息管理-|n"); printf("|-1.基本信息插入.-2.基本信息修改-|n"); printf("|-3.基本信息删除.-4.基本信息显示-|n"); printf("|-r.初始化(第一次使用时用!慎用)-|n"); printf("|-请输入你的选

17、择:-|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");return; if(pid=0)/child.read(pipefiles0,&ch,1);switch(ch)case '1':while(1)sleep(1);len=sizeof(struct stu_info);char

18、*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,sizeof(unsigned int),1,out);fclose(out);if(out=fopen("c.doc","a&quo

19、t;)=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,si

20、zeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_info);fread(stu,len,number,in);unsigned int j;for(j=0;j<number;j+)if(strcmp(student.stu_num,stuj.stu_num)=0)strcpy(,);stuj.sex=student.sex;strcpy(stuj.mobile_phone,student.mobile_phone);break;fclose(in);i

21、f(j=number)printf("没有这条记录n");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_

22、num);read(pipefiles0,stu_num,sizeof(stu_num);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;for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number)

23、 printf("没有这条记录n");else for(;i<number-1;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(,stui+1.name);stui.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(

24、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);char f;read(pipefiles0,&f,sizeof(int);if(f!='y'&&f!='Y') return;break;case '4':sle

25、ep(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;i<number;i+)printf("-%d-学号: %s-姓名: %s-性别<0是女,1是男>: %c-手机号: %s-n"

26、,i,stui.stu_num,,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');wri

27、te(pipefiles1,&ch,1);switch(ch)case '1':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",);print

28、f("性别0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手机号:");scanf("%s",student.mobile_phone);len=sizeof(struct stu_info);write(pipefiles1,&student,len);sleep(1);printf("是否继续输入YNn");fflush(stdin);dop=getchar();while(p='n');

29、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",);printf("性别0是女,1是男:");dostudent.

30、sex=getchar();while(student.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

31、(stu_num,0,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);write(pipefiles1,stu_num,sizeof(stu_num);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;ca

32、se '4':printf("n-显示学生信息-n");sleep(1);break;case 'r':printf("ninit student tablen");sleep(2);break;default:break; 学习并使用线程间的同步方法,重新改写实验3步骤6要求的程序。要求启用两个线程,其中主线程接受用户对文件的操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)。#include<stdio.h>#include<unistd.h>#include

33、<string.h>#include<stdlib.h>#include<time.h>#include<pthread.h>#include <semaphore.h>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

34、bin_sem;sem_t bin_sem1;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(&qu

35、ot;|-r.初始化(第一次使用时用!慎用)-q.退出-|n");printf("|-请输入你的选择:-|n");printf("-n");void main()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(&am

36、p;bin_sem1,0,0);if (res != 0)perror("Semaphore initialition failed");exit(EXIT_FAILURE);res = pthread_create(&a_thread,NULL,stuthread,NULL);if(res != 0)perror("Thread creation failed");exit(EXIT_FAILURE);while(1)doch=getchar();while(ch='n');if(ch='q')sem_post(

37、&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,&thread_result);if(res != 0)perror("Thread join failed");exit(EXIT_FAILURE);se

38、m_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();break;case 

39、9;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

40、()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",);printf("性别0是女,1是男:");dostudent.sex=getchar();

41、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 i

42、nt),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);sca

43、nf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性别0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手机号:");scanf("%s",student.mobile_phone);len=sizeof(struct stu_info);in=fopen("c.doc&quo

44、t;,"r+");fread(&number,sizeof(unsigned int),1,in);unsigned int j;long kk;for(j=0;j<number;j+)fread(&stu0,len,1,in);if(strcmp(student.stu_num,stu0.stu_num)=0)kk=len;fseek(in,-kk,SEEK_CUR);fwrite(&student,len,1,in);break;fflush(in);fclose(in);if(j=number)printf("没有这条记录n&q

45、uot;);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("请输入删除学生的学号:");char stu_num12;memse

46、t(stu_num,0,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number) printf("没有这条记录n");else number-;for(;i<number;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(,stui+1.name);stui.sex=stui+1.sex;strcp

47、y(stui.mobile_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

48、,1,out);fflush(out);fclose(out);void displaystu()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;printf("-n");printf("numb

49、er t学号t 姓名t性别<0是女,1是男> 手机号n");for(i=0;i<number;i+)printf(" %3d %12s%10s %c %12sn",i,stui.stu_num,,stui.sex,stui.mobile_phone);printf("-n");fclose(in); 学习并使用IPC中信号量和共享内存的使用方法,重新改写实验3步骤6要求的程序。要求启动多个进程,每一个进程都可以单独对文件进行操作,进程间通过信号量进行同步,对文件的操作映射到共享内存中。#include<

50、stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>#include <sys/shm.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/sem.h>#include "semun.h"struct stu_info char stu_num12;char name10;char sex;/*0为女生,1为

51、男生*/char mobile_phone12;struct stu_info student;unsigned int number;struct stu_info stu100;FILE *in;FILE *out;int len;static int sem_id;void inputstu();void updatestu();void deletestu();void displaystu();void set_semvalue();void del_semvalue();void semaphore_p();void semaphore_v();void commanddisp()printf("-n&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论