版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、學(xué)習(xí)并分別使用標(biāo)準(zhǔn)C的文件操作函數(shù)和Linux的系統(tǒng)調(diào)用創(chuàng)建一個(gè)對(duì)學(xué)生基本信息進(jìn)行操作(插入、修改和刪除)的C程序,學(xué)生基本信息以結(jié)構(gòu)體的形式存儲(chǔ)在文件#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("|-學(xué)生基本信息管理-|n");printf("|-1.基本信息插入.-2.基本信息修改-|n");printf("|-3.基本信息刪除.-4.基本信息顯示-|n&quo
3、t;);printf("|-r.初始化(第一次使用時(shí)用!慎用)-q.退出-|n");printf("|-請(qǐng)輸入你的選擇:-|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-請(qǐng)輸入學(xué)生信息-n");char p='y'while(p='y'|p='Y')memset(&student,0,sizeof(student);printf("學(xué)號(hào):");scanf("%s",student.stu_num);printf(&
6、quot;姓名:");scanf("%s",);printf("性別0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手機(jī)號(hào):");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("是否繼續(xù)輸入YNn");fflush(stdin);dop=getchar();while(p='n');void updatestu
8、()printf("n-學(xué)生信息修改-n");printf("請(qǐng)輸入修改學(xué)生的學(xué)號(hào):");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、機(jī)號(hào):");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-學(xué)生信息刪除-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("請(qǐng)輸入刪除學(xué)生的學(xué)號(hào):");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("是否繼續(xù)刪除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-顯示學(xué)生信息-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學(xué)號(hào)t 姓名t性別<0是女,1是男> 手機(jī)號(hào)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、利用父子進(jìn)程間的管道通信方式,改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟用兩個(gè)進(jìn)程,其中父進(jìn)程接受用戶對(duì)文件的操作命令然后通過管道發(fā)給子進(jìn)程,子進(jìn)程完成對(duì)文件的實(shí)際操作。#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("|-學(xué)生基本信息管理-|n"); printf("|-1.基本信息插入.-2.基本信息修改-|n"); printf("|-3.基本信息刪除.-4.基本信息顯示-|n"); printf("|-r.初始化(第一次使用時(shí)用!慎用)-|n"); printf("|-請(qǐng)輸入你的選
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-學(xué)號(hào): %s-姓名: %s-性別<0是女,1是男>: %c-手機(jī)號(hào): %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-請(qǐng)輸入學(xué)生信息-n");char p='y'while(p='y'|p='Y')memset(&student,0,sizeof(student);printf("學(xué)號(hào):");scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);print
28、f("性別0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手機(jī)號(hào):");scanf("%s",student.mobile_phone);len=sizeof(struct stu_info);write(pipefiles1,&student,len);sleep(1);printf("是否繼續(xù)輸入YNn");fflush(stdin);dop=getchar();while(p='n');
29、write(pipefiles1,&p,sizeof(char); break;case '2':printf("n-學(xué)生信息修改-n");printf("請(qǐng)輸入修改學(xué)生的學(xué)號(hào):");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("手機(jī)號(hào):");scanf("%s",student.mobile_phone);len=sizeof(struct stu_info);write(pipefiles1,&student,len);sleep(2);break;case '3':printf("n-學(xué)生信息刪除-n");while(1)printf("請(qǐng)輸入刪除學(xué)生的學(xué)號(hào):");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("是否繼續(xù)刪除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-顯示學(xué)生信息-n");sleep(1);break;case 'r':printf("ninit student tablen");sleep(2);break;default:break; 學(xué)習(xí)并使用線程間的同步方法,重新改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟用兩個(gè)線程,其中主線程接受用戶對(duì)文件的操作命令然后發(fā)給子線程,子線程完成對(duì)文件的實(shí)際操作(注意線程間的同步)。#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("|-學(xué)生基本信息管理-|n");printf("|-1.基本信息插入.-2.基本信息修改-|n");printf("|-3.基本信息刪除.-4.基本信息顯示-|n");printf(&qu
35、ot;|-r.初始化(第一次使用時(shí)用!慎用)-q.退出-|n");printf("|-請(qǐng)輸入你的選擇:-|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-請(qǐng)輸入學(xué)生信息-n");char p='y'while(p='y'|p='Y')memset(&student,0,sizeof(student);printf("學(xué)號(hào):");scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別0是女,1是男:");dostudent.sex=getchar();
41、while(student.sex='n');printf("手機(jī)號(hào):");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("是否繼續(xù)輸入YNn");fflush(stdin);dop=getchar();while(p='n');void updatestu()printf("n-學(xué)生信息修改-n");printf("請(qǐng)輸入修改學(xué)生的學(xué)號(hào):");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("手機(jī)號(hào):");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-學(xué)生信息刪除-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("請(qǐng)輸入刪除學(xué)生的學(xué)號(hào):");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("是否繼續(xù)刪除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-顯示學(xué)生信息-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學(xué)號(hào)t 姓名t性別<0是女,1是男> 手機(jī)號(hào)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); 學(xué)習(xí)并使用IPC中信號(hào)量和共享內(nèi)存的使用方法,重新改寫實(shí)驗(yàn)3步驟6要求的程序。要求啟動(dòng)多個(gè)進(jìn)程,每一個(gè)進(jìn)程都可以單獨(dú)對(duì)文件進(jìn)行操作,進(jìn)程間通過信號(hào)量進(jìn)行同步,對(duì)文件的操作映射到共享內(nèi)存中。#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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年某醫(yī)藥公司與某藥店關(guān)于產(chǎn)品銷售的合同
- 2024年版?zhèn)€人信用擔(dān)保借款合同版B版
- 2024年櫥柜品牌授權(quán)合同2篇
- 2024版廢舊木材資源回收與家具制造出口合同3篇
- 2024版建筑防水工程防水材料質(zhì)量監(jiān)督與檢測(cè)合同3篇
- 2024年版大型風(fēng)機(jī)購(gòu)銷與安裝合同
- 2024版二手車貸款逾期貸款處置合同2篇
- 2024年度個(gè)人股權(quán)轉(zhuǎn)讓合同書含股權(quán)激勵(lì)及員工持股計(jì)劃范本6篇
- 2024年環(huán)保技術(shù)研發(fā)與應(yīng)用承包合同綠色發(fā)展強(qiáng)推
- 2024版地產(chǎn)項(xiàng)目品牌重塑推廣合同范本3篇
- 2024年智慧城市建設(shè)中的市政基礎(chǔ)設(shè)施智能化改造
- 《18.2 矩形的性質(zhì)、矩形的判定》課件(含習(xí)題)
- 開診所可行性分析報(bào)告
- 幼兒園唐詩三百首
- 洗車設(shè)備檢驗(yàn)報(bào)告
- 設(shè)備操作手冊(cè)編寫與更新方法和技巧講解與實(shí)操演示
- 強(qiáng)軍夢(mèng)小品劇本-中國(guó)夢(mèng)強(qiáng)軍夢(mèng)小品(我的強(qiáng)軍夢(mèng))
- 心力衰竭治療中的體外機(jī)械循環(huán)輔助
- (新湘教版)地理 八年級(jí)上冊(cè)+下冊(cè) 填圖課件
- 福建省服務(wù)區(qū)標(biāo)準(zhǔn)化設(shè)計(jì)指南
- 益豐大藥房入職測(cè)評(píng)題庫(kù)
評(píng)論
0/150
提交評(píng)論