




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第9章 文件一、選擇題1. C2. B3. A二、填空題1. 二進制文件2. 順序 隨機 流 流式3. hello,4. picursound三、簡答題1. 什么是文件?文件分為哪些類型?答:文件一般是指存儲在計算機外部介質(zhì)上的數(shù)據(jù)的集合。文件分為兩種類型:ASCII文件和二進制文件。2. 什么是緩沖文件系統(tǒng)?什么是非緩沖文件系統(tǒng)?答:緩沖文件系統(tǒng)是指系統(tǒng)自動地在內(nèi)存區(qū)為每個正在使用的文件開辟一個緩沖區(qū)。從內(nèi)存向磁盤輸出數(shù)據(jù)時,先將數(shù)據(jù)送入輸出文件緩沖區(qū)中,當輸出文件緩沖區(qū)寫滿時,再一起寫到磁盤上。如果從磁盤向內(nèi)存讀入數(shù)據(jù),則一次從磁盤文件將一批數(shù)據(jù)讀取到輸入緩沖區(qū),然后再從緩沖區(qū)逐個地將數(shù)據(jù)
2、送到程序數(shù)據(jù)區(qū)。非緩沖文件系統(tǒng)是指系統(tǒng)不自動開辟確定大小的緩沖區(qū),而由程序為每個文件設(shè)定緩沖區(qū)。3. 如何理解文件的打開和關(guān)閉?為什么要打開和關(guān)閉文件?答:打開文件,是指建立文件與對應(yīng)緩沖區(qū)之間的聯(lián)系,即建立與文件對應(yīng)的信息表。關(guān)閉文件,是指撤銷與操作文件相關(guān)的流。即通過關(guān)閉操作,通知系統(tǒng)釋放相應(yīng)的文件信息區(qū)。這樣,原來的文件指針變量不再指向該文件,此后,也就不可能通過此指針來訪問該文件。要對文件操作必須打開文件,即建立與文件對應(yīng)的信息表。而如果不關(guān)閉文件將會丟失數(shù)據(jù)。4. 簡述文件指針的概念。如何通過文件指針訪問一個文件?答:文件指針指的是指向文件結(jié)構(gòu)體的指針變量。要通過文件指針訪問一個文件
3、,必須先定義這個文件指針,然后打開文件即可。例如:FILE *fp;fp=fopen(d:exam1.txt,r)就可以通過文件指針訪問d:exam1.txt文件。四、編程題1. 參考程序如下。#include #includevoid main(void)float data;int i,num=0;char ch;FILE *fp;if(fp=fopen(8_5.txt,w+)=NULL)printf(cannot open filen);exit(1); printf(請輸入10個浮點數(shù):n);for(i=1;i=10;i+)scanf(%f,&data);fprintf(fp,%f,d
4、ata);printf(請輸入若干個浮點數(shù),以#結(jié)束:n);while(ch=getchar()!=#)scanf(%f,&data);fprintf(fp,%f,data); printf(文件中的數(shù)據(jù)為:n);rewind(fp);while(!feof(fp)fscanf(fp,%f,&data);printf(%f ,data);num+;num-;printf(文件中數(shù)據(jù)的個數(shù)為%dn,num);fclose(fp);2. 參考程序如下。#include #include int main ()FILE *in1,*in2,*out;if(in1=fopen(data1,r)=NUL
5、L)printf(cannot open data1n);exit(0);if(out=fopen(data3,w)=NULL)printf(cannot open data3n);exit(0);while(!feof(in1)fputc(fgetc(in1),out);if(in2=fopen(data2,r)=NULL)printf(cannot open data2n);exit(0);while(!feof(in2)fputc(fgetc(in2),out);fclose(in1);fclose(in2);fclose(out); return 0;3. 參考程序如下。#includ
6、e#includevoid main()FILE *fp;char ch;if(fp=fopen(file3.txt,w)=NULL)printf(cannot open file n);exit(0);printf(請輸入一個字符串,以#結(jié)束n);ch=getchar();while(ch!=#)fputc(ch,fp);ch=getchar();fclose(fp);4. 參考程序如下。#include #include void main()FILE *in,*out;char ch,infile10,outfile10;printf(請輸入原文件名:n);scanf(%s,infile
7、);printf(請輸入轉(zhuǎn)換后文件名:n);scanf(%s,outfile);if (in=fopen(infile,r)=NULL)printf(cannot open infilen);exit(0);if (out=fopen(outfile,w)=NULL)printf(cannot open outfilen);exit(0);while(!feof(in)ch=fgetc(in);if(ch=a&ch=z) ch=ch-32;fputc(ch,out);fclose(in);fclose(out);5. 參考程序如下。#include#include#includevoid ma
8、in()FILE *in;char ch,infile10,array100;int i=0;printf(請輸入文件名:n);scanf(%s,infile);if (in=fopen(infile,r)=NULL)printf(cannot open outfilen);exit(0);while(!feof(in)arrayi=fgetc(in);i+;arrayi=0; printf(倒序打印為:n);for(i=strlen(array)-1;i=0;i-)printf(%c,arrayi);fclose(in);6. 參考程序如下。#include#include#define N
9、 10struct student char name9; int num; int age; char sex;stuN;void main() int i; char s10; FILE *fp; if(fp=fopen(stu,rb)=NULL) printf(Can not open file!n); exit(0); gets(s); for(i=0;iN;i+) fseek(fp,i*sizeof(struct student),0); fread(&stui,sizeof(struct student),1,fp); while(s=) printf(%s,%d
10、,%d,%dn,,stui.num,stui.age,stui.sex); fclose(fp);7. 參考程序如下。#include stdio.hmain() FILE *fp; int i,j,n; char c160,t,ch; if(fp=fopen(A,r)=NULL) printf(file A cannot be openedn); exit(0); printf(n A contents are :n); for(i=0;(ch=fgetc(fp)!=EOF;i+) ci=ch;putchar(ci); fclose(fp); if(fp=fopen(B,r
11、)=NULL) printf(file B cannot be openedn); exit(0); printf(n B contents are :n); for(i=0;(ch=fgetc(fp)!=EOF;i+) ci=ch; putchar(ci); fclose(fp); n=i; for(i=0;in;i+) for(j=i+1;jcj) t=ci;ci=cj;cj=t; printf(n C file is:n); fp=fopen(C,w); for(i=0;in;i+) putc(ci,fp); putchar(ci); fclose(fp);8. 參考程序如下。#incl
12、ude#include#define N 30void main() FILE *fp; char c,fileN; scanf(%s,file); if(fp=fopen(file,w)=NULL) printf(Can not open filen); exit(0); c=getchar(); c=getchar(); while(c!=n) fputc(c,fp); putchar(c); c=getchar(); putchar(N); fclose(fp);9. 參考程序如下。#include stdio.hstruct student long int num; char nam
13、e10; int age; char speciality20; ; FILE *fp; main() struct student st; fp=fopen(student.dat,rb); if(fp=NULL) printf(file not foundn); else while(!feof(fp) fread(&st,sizeof(struct student),1,fp); if(st.num=970101&st.num=970135) printf(%ld,%s,%d,%sn,st.num,,st.age,st.speciality); 10. 參考程序如下。#in
14、clude #include struct clerkchar num6;char name8;char sex2;int age;double salary;clk10,clkread10;void main()int i,sum;FILE *fp;/* 輸入 */for (i=0;i10;i+ )printf(n請輸入職工%d的數(shù)據(jù):n,i+1);printf(職工號:);scanf(%s,clki.num);printf(職工姓名:);scanf(%s,);printf(性別:);scanf(%s,clki.sex);printf(年齡:);scanf(%d,&clki
15、.age);printf(工資:);scanf(%d,&clki.salary);/*將數(shù)據(jù)寫入文件*/fp=fopen(worker.dat,w);for (i=0;i10;i+ )if (fwrite(&clki,sizeof(struct clerk),1,fp)!=1)printf (file write errorn);fclose(fp);/*讀文件*/if (fp=fopen(worker.dat,r)=NULL)printf(文件打不開);exit(0);printf(n文件內(nèi)容是:n); printf(%8s%8s%8s%8s%8s,職工號,職工姓名,性別,年齡,工資);fo
16、r (i=0;fread (&clkreadi,sizeof(struct clerk),1,fp)!=0;i+)printf(n%8s,clkreadi.num);printf(%8s,);printf(%8s,clkreadi.sex);printf(%8d,clkreadi.age);printf(%8d,clkreadi.salary);fclose(fp);11. 參考程序如下。#includestruct student char num6;char name8;int score3;float avr;stu5;void main()int i,j,su
17、m;FILE *fp;/* 輸入 */for (i=0;i5 ;i+ )printf(n請輸入學生%d的成績:n,i+1);printf(學號:);scanf(%s,stui.num);printf(姓名:);scanf(%s,);sum=0;for (j=0;j3;j+ )printf(成績 %d:,j+1);scanf(%d,&stui.scorej);sum+=stui.scorej;stui.avr=(float)(sum/3.0);/*將數(shù)據(jù)寫入文件*/fp=fopen(student,wb);for (i=0;i5;i+ )if (fwrite(&stui,siz
18、eof(struct student),1,fp)!=1)printf (file write errorn);fclose(fp);12. 參考程序如下。#include#include #define N 10struct studentchar num6;char name8;int score3;float avr;stN,change;void main() FILE *fp;int i,j,n;/*讀文件*/if (fp=fopen(student,r)=NULL)printf(文件打不開);exit(0);printf(n文件內(nèi)容是:);for(i=0;fread(&sti,sizeof(struct student),1,fp)!=0;i+)printf(n%8s%8s,sti.num,);for (j=0;j3 ;j+ )printf (%8d,sti.scorej);printf (%10.2f,sti.avr);fclose(fp);/*排序*/for(i=0;in
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高端住宅裝修包工包料合同范本
- 精裝房臺面改造方案
- 網(wǎng)絡(luò)抽簽面試題及答案
- 母嬰護理考試題及答案
- 水彩臨摹考試題及答案
- 夜市火災(zāi)處置預(yù)案方案
- 2026版《全品高考》選考復(fù)習方案生物806 第25講 體液調(diào)節(jié)與神經(jīng)調(diào)節(jié)的關(guān)含答案
- 學校周邊攤點飲食健康現(xiàn)狀與對策分析
- 營銷策劃方案執(zhí)行
- 市區(qū)廢棄廠房拆除方案
- 高校各級黨組織和廣大黨員在網(wǎng)絡(luò)空間發(fā)揮作用研究
- 2025年 濟南綜??毓杉瘓F有限公司招聘考試試卷附答案
- 中國混凝土攪拌站行業(yè)發(fā)展前景及發(fā)展策略與投資風險研究報告2025-2028版
- 2025年云南省中考化學真題(解析版)
- 2025年人工智能基礎(chǔ)及應(yīng)用考試試題及答案
- 化妝初期培訓(xùn)課件
- 2025年東航食品招聘筆試參考題庫含答案解析
- 公司業(yè)績考核管理制度
- 餐廳運營與管理制度
- DB31/T 908-2018地理標志產(chǎn)品松江大米
- 教育改革背景下的中醫(yī)師承教育新思路
評論
0/150
提交評論