c語(yǔ)言上機(jī)實(shí)驗(yàn)12_第1頁(yè)
c語(yǔ)言上機(jī)實(shí)驗(yàn)12_第2頁(yè)
c語(yǔ)言上機(jī)實(shí)驗(yàn)12_第3頁(yè)
c語(yǔ)言上機(jī)實(shí)驗(yàn)12_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)驗(yàn)十二文件的操作(二)一.實(shí)驗(yàn)?zāi)康恼莆瘴募羔樀幕靖拍钆c定義方法;掌握對(duì)文件進(jìn)行讀寫、定位等的操作方法。二.實(shí)驗(yàn)環(huán)境硬件:PII以上計(jì)算機(jī);軟件:Windows、VisualC++6.0;其它:一張軟盤或者U盤等可移動(dòng)的存儲(chǔ)設(shè)備。三.實(shí)驗(yàn)內(nèi)容練習(xí)1.有5個(gè)學(xué)生,每個(gè)學(xué)生的數(shù)據(jù)包括學(xué)號(hào)、姓名、三門課的成績(jī),從鍵盤輸入5個(gè)學(xué)生的數(shù)據(jù),要求:⑴將讀入的數(shù)據(jù)存入磁盤文件“stu.txt”中;⑵從磁盤文件“stu.txt”中讀出并顯示所有學(xué)生數(shù)據(jù);⑶將指定課程、指定分?jǐn)?shù)段范圍內(nèi)的學(xué)生數(shù)據(jù)另存入磁盤文件“range.txt”中;⑷從磁盤文件“range.txt”中讀出并顯示學(xué)生數(shù)據(jù)。⑸用記事本打開磁盤文件“stu.txt”和“range.txt”驗(yàn)證其內(nèi)容。要求:使用fscanf和fprintf函數(shù)。運(yùn)行結(jié)果示例:程序主框架:#include"stdlib.h"#include"iostream.h"#include"stdio.h"#defineFORMAT"%d%s%d%d%d"http://定義格式控制符#defineN5structstudent{ intnum; charname[20]; intscore[3];}stu[N];voidinput(chara[])//a用于接收要寫入數(shù)據(jù)的文件名{}voidoutput(chara[],intsn)//sn用于接收要輸出文件中的學(xué)生人數(shù){}intselect(chara[],charb[])//函數(shù)返回值為挑選出來(lái)的學(xué)生人數(shù){}voidmain(){ intsn; input("stu.txt"); output("stu.txt",N); sn=select("stu.txt","range.txt"); output("range.txt",sn);}答案1.#include<stdlib.h>#include<iostream.h>#include<stdio.h>#defineFORMAT"%d%s%d%d%d"#defineN5structstudent{intnum;charname[20];intscore[3];}stu[N];voidinput(chara[]){FILE*fp1;inti;if((fp1=fopen(a,"w"))==NULL){cout<<"cannotopenfile"<<endl;exit(0);}cout<<"輸入學(xué)生的數(shù)據(jù):"<<endl;for(i=0;i<N;i++){ cin>>stu[i].num>>stu[i].name>>stu[i].score[0]>>stu[i].score[1]>>stu[i].score[2];fprintf(fp1,FORMAT,stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]); fprintf(fp1,"%c","\n");}fclose(fp1);}voidoutput(chara[],intsn){FILE*fp1;inti;if((fp1=fopen(a,"r"))==NULL){cout<<"cannotopenfile"<<endl;exit(0);}cout<<endl<<"輸出學(xué)生的數(shù)據(jù):"<<endl;for(i=0;i<sn;i++){fscanf(fp1,FORMAT,&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);cout<<stu[i].num<<""<<stu[i].name<<""<<stu[i].score[0]<<""<<stu[i].score[1]<<""<<stu[i].score[2];cout<<endl;}cout<<endl;fclose(fp1);}intselect(chara[],charb[]){intx,y,z,i,n=0;FILE*fp1,*fp2;cout<<"輸入課程號(hào)(1-3):";cin>>x;cout<<"輸入分?jǐn)?shù)段(XX-YY):";cin>>y>>z;if((fp1=fopen(a,"r"))==NULL){cout<<"cannotopenfile"<<endl;exit(0);}if((fp2=fopen(b,"w"))==NULL){cout<<"cannotopenfile"<<endl;exit(0);}for(i=0;i<N;i++){ if(stu[i].score[x-1]>y&&stu[i].score[x-1]<z) { fscanf(fp1,FORMAT,&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);fprintf(fp2,FORMAT,stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);fprintf(fp2,"%c","\n");n++; }}fclose(fp1);fclose(fp2);return

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論