第十章 習題及答案_第1頁
第十章 習題及答案_第2頁
第十章 習題及答案_第3頁
第十章 習題及答案_第4頁
第十章 習題及答案_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第十章 習題一、選擇題1.以下敘述中正確的是_。A)C語言中的文件是流式文件,因此只能順序存取數(shù)據(jù)B)打開一個已存在的文件并進行了寫操作后,原有文件中的全部數(shù)據(jù)必定被覆蓋C)在一個程序中當對文件進行了寫操作后,必須先關閉該文件然后再打開,才能讀到第1個數(shù)據(jù)D)當對文件的讀(寫)操作完成之后,必須將它關閉,否則可能導致數(shù)據(jù)丟失2.當已存在一個abc.txt文件時,執(zhí)行函數(shù)fopen (“abc.txt”, “r+”)的功能是 。A)打開abc.txt文件,清除原有的內容B)打開abc.txt文件,只能寫入新的內容C)打開abc.txt文件,只能讀取原有內容D)打開abc.txt文件,可以讀取和寫

2、入新的內容3.若fp是指向某文件的指針,且已讀到此文件末尾,則庫函數(shù)feof(fp)的返回值是 。A) EOF B) 0 C) 非零值 D) NULL4.以下程序企圖把從終端輸入的字符輸出到名為abc.txt的文件中,直到從終端讀入字符#號時結束輸入和輸出操作,但程序有錯。#include main() FILE *fout; char ch;fout=fopen(abc.txt,w);ch=fgetc(stdin);while(ch!=#) fputc(ch,fout);ch =fgetc(stdin);fclose(fout);出錯的原因是 。A) 函數(shù)fopen調用形式有誤 B) 輸入文

3、件沒有關閉 C) 函數(shù)fgetc調用形式有誤 D) 文件指針stdin沒有定義5.有以下程序 #include main() FILE *pf;char *s1=China,*s2=Beijing;pf=fopen(abc.dat,wb+);fwrite(s2,7,1,pf);rewind(pf);fwrite(s1,5,1,pf);fclose(pf);以下程序執(zhí)行后abc.dat文件的內容是A)China B)Chinang C)ChinaBeijing D)BeijingChina6有以下程序#include main ()FILE *fp; int i,a6=1,2,3,4,5,6;

4、fp=fopen(“d3.dat”,”w+b”);fwrite(a,sizeof(int),6,fp);fseek(fp,sizeof(int)*3,SEEK_SET); fread(a,sizeof(int),3,fp); fclose(fp);for(i=0;i6;i+) printf(“%d,”,ai);程序運行后的輸出結果是( )A)4,5,6,4,5,6, B)1,2,3,4,5,6, C)4,5,6,1,2,3, D)6,5,4,3,2,1,7.有以下程序 #include main() FILE *fp; int a10=1,2,3,i,n; fp=fopen(“dl.dat”,

5、”w”); for(i=0;i3;i+) fprintf(fp,”%d”,ai); fprintf(fp,”n”); fclose(fp); fp=fopen(“dl.dat”,”r”); fscanf(fp,”%d”,&n); fclose(fp); printf(“%dn”,n); 程序的運行結果是 A)12300 B) 123 C) 1 D) 3218.設有以下結構體類型:struct st char name8; int num; float s4; student 20;并且結構體數(shù)組student中的元素都已經(jīng)有值,若要將這些元素寫到fp所指向的磁盤文件中,以下不正確的形式是()。

6、A)fwrite (student , sizeof(struct st ),20 , fp);B)fwrite (student ,20* sizeof(struct st ),1, fp);C)fwrite (student , 10*sizeof(struct st ),10 , fp);D)for (i=0;i=a&stri=z) stri=_(2)_; fputc(stri,fp); i+; fclose(fp); fp=fopen(test.txt,_(3)_); fgets(str,100,fp); printf(%sn,str); fclose(fp);4.下面程序用變量cou

7、nt統(tǒng)計文件中字符的個數(shù)。請?zhí)羁铡? include main( ) FILE *fp; long count=0; if(fp=fopen(“l(fā)etter.dat”, (1) )= =NULL) printf(“cannot open filen”); exit(0); while(!feof(fp) (2) ; (3) ; printf(“count=%ldn”, count); fclose(fp); 5.以下程序的功能是將文件file1.c的內容輸出到屏幕上并復制到文件file2.c中。請?zhí)羁铡?# include main( ) FILE (1) ; fp1= fopen(“file

8、1.c”, ”r”); fp2= fopen(“file2.c”, ”w”); while(!feof(fp1) putchar(getc(fp1); (2) while(!feof(fp1) putc( (3) ); fclose(fp1); fclose(fp2); 6.以下程序段打開文件后,先利用 fseek函數(shù)將文件位置指針定位在文件末尾,然后調用ftell函數(shù)返回當前文件位置指針的具體位置,從而確定文件長度,請?zhí)羁?。FILE *myf; long f1;myf= fopen(“test.t”,“rb”); ;f1=ftell(myf);fclose(myf);printf(“%dn”

9、,f1); 三、編程題1. 編寫一個程序,建立一個abc文本文件,向其中寫入“this is a test”字符串,然后顯示該文件的內容。2. 有5個學生,每個學生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學生號、姓名、三門課成績),計算出平均成績,將原有數(shù)據(jù)和計算出的平均分數(shù)存放在磁盤文件stud中。3. 將上題stud文件中的學生數(shù)據(jù)按平均分進行排序處理,并將已排序的學生數(shù)據(jù)存入一個新文件stu-sort中。4. 將上題以排序的學生成績文件進行插入處理。插入一個學生的3門課成績,程序先計算新插入學生的平均成績,然后將它按平均成績高低順序插入,插入后建立一個新文件。第十章 習題及答案一、選擇題

10、1-8 D D A A B A B C二、填空題1.文本文件、二進制文件 2. 字節(jié) 流式3. (1)w或w+或wt或w+t或wt+(2) stri-32(3)r或r+或r+t或rt+4. (1) r (2)fgetc(fp) (3) count+5. (1)*fp1,*fp2 (2)rewind(fp1);(3)getc(fp1),fp26. fseek(myf,0,SEEK_END)三、編程題1. 編寫一個程序,建立一個abc文本文件,向其中寫入“this is a test”字符串,然后顯示該文件的內容。#include #include #includemain( )FILE *fp;

11、 char msg = this is a test; char buf20; if(fp=fopen(abc,w+)=NULL) printf(不能建立abc文件n); exit(0); fwrite(msg,strlen(msg)+1,1,fp);fseek(fp,SEEK_SET,0);fread(buf,strlen(msg)+1,1,fp);printf(%sn,buf);fclose(fp);2.有5個學生,每個學生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學生號、姓名、三門課成績),計算出平均成績,將原有數(shù)據(jù)和計算出的平均分數(shù)存放在磁盤文件stud中。 #include struc

12、t student char num10; char name8; int score3; float ave; stu5; main() int i,j,sum; FILE *fp; for(i=0;i5;i+) printf(n input score of student%d:n,i+1); printf(NO.:); scanf(%s,stui.num); printf(name:); scanf(%s,); sum=0; for(j=0;j3;j+) printf(score %d :,j+1); scanf(%d,&stui.scorej); sum+=stui.

13、scorej; stui.ave=sum/3.0; fp=fopen(stud,w); for(i=0;i5;i+) if(fwrite(&stui,sizeof(struct student),1,fp)!=1) printf(File write errorn); fclose(fp); fp=fopen(stud,r); for(i=0;i5;i+) fread(&stui,sizeof(struct student),1,fp); printf(%s,%s,%d,%d,%d,%6.2fn,stui.num,,stui.score0, stui.score1, stui

14、.score2 ,stui.ave); 3.將上題stud文件中的學生數(shù)據(jù)按平均分進行排序處理,并將已排序的學生數(shù)據(jù)存入一個新文件stu-sort中。 #include #include#define N 10 struct student char num10; char name8; int score3; float ave; stN,temp; main() FILE *fp; int i,j,n; if(fp=fopen(stud,r)=NULL) printf(can not open the file); exit(0); printf(n file stud:); for(i=

15、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.f,sti.ave); fclose(fp); n=i; for(i=0;in;i+) for(j=i+1;jn;j+) if(sti.avestj.ave) temp=sti; sti=stj; stj=temp; printf(nnow:); fp=fopen(stu-sort,w); for(i=0;in;i+) fwrite

16、(&sti,sizeof(struct student),1,fp); printf(n%8s%8s,sti.num,); for(j=0;j3;j+) printf(%8d,sti.scorej); printf(%10.2f,sti.ave); fclose(fp); 4.將上題以排序的學生成績文件進行插入處理。插入一個學生的3門課成績,程序先計算新插入學生的平均成績,然后將它按平均成績高低順序插入,插入后建立一個新文件。 #include #includestruct student char num10; char name8; int score3; float av

17、e; st10,s; main() FILE *fp, * fp1; int i,j,t,n; printf(n NO.:); scanf(%s,s.num); printf(name:); scanf(%s,); printf(score1,score2,score3:); scanf(%d,%d,%d,&s.score0, &s.score1, &s.score2); s.ave=(s.score0+s.score1+s.score2)/3.0; if(fp=fopen(stu_sort,r)=NULL) printf(can not open file.); exit(0);

18、 printf(original data:n); for(i=0;fread(&sti,sizeof(struct student),1,fp)!=0;i+) printf(n%8s%8s,sti.num,); for(j=0;js.ave&tn;t+); printf(nnow:n); fp1=fopen(sort1.dat,w); for(i=0;it;i+) fwrite(&sti,sizeof(struct student),1,fp1); printf(n%8s%8s,sti.num,); for(j=0;j3;j+) printf(%8d,sti.scorej); printf(%10.2f,sti.ave); fwrite(&s,siz

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論