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

下載本文檔

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

文檔簡介

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

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

3、=fgetc(stdin);fclose(fout);出錯的原因是 。A) 函數(shù)fopen調(diào)用形式有誤 B) 輸入文件沒有關(guān)閉 C) 函數(shù)fgetc調(diào)用形式有誤 D) 文件指針stdin沒有定義5.有以下程序 #include <stdio.h>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文件的內(nèi)容是A)China B)Chinang C)ChinaBei

4、jing D)BeijingChina6有以下程序#include <stdio.h>    main ()    FILE *fp; int i,a6=1,2,3,4,5,6;     fp=fopen(“d3.dat”,”w+b”);     fwrite(a,sizeof(int),6,fp);     fseek(fp,sizeof(int)*3,SEEK_

5、SET);      fread(a,sizeof(int),3,fp); fclose(fp);     for(i=0;i<6;i+) printf(“%d,”,ai);    程序運行后的輸出結(jié)果是( )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 <stdio.h> main() FILE

6、*fp; int a10=1,2,3,i,n; fp=fopen(“dl.dat”,”w”); for(i=0;i<3;i+) fprintf(fp,”%d”,ai); fprintf(fp,”n”); fclose(fp); fp=fopen(“dl.dat”,”r”); fscanf(fp,”%d”,&n); fclose(fp); printf(“%dn”,n); 程序的運行結(jié)果是 A)12300 B) 123 C) 1 D) 3218.設(shè)有以下結(jié)構(gòu)體類型:struct st char name8; int num; float s4; student 20;并且結(jié)構(gòu)體數(shù)組

7、student中的元素都已經(jīng)有值,若要將這些元素寫到fp所指向的磁盤文件中,以下不正確的形式是()。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<20;i+)fwrite (student+i , sizeof(struct st ),1 , fp);二、填空題1.C語言中根據(jù)數(shù)據(jù)的組織形式,把文件分為 和 兩種。2

8、.在C語言中,文件的存取是以 為單位的,這種文件被稱作 文件。3.以下程序的功能是:從鍵盤上輸入一個字符串, 把該字符串中的小寫字母轉(zhuǎn)換為大寫字母,輸出到文件test.txt中,然后從該文件讀出字符串并顯示出來。請?zhí)羁铡?#include main() FILE *fp; char str100; int i=0; if(fp=fopen("text.txt",_(1)_)=NULL) printf("can't open this file.n");exit(0); printf("input astring:n"); ge

9、ts(str); while (stri) if(stri>='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.下面程序用變量count統(tǒng)計文件中字符的個數(shù)。請?zhí)羁铡? include <stdio.h> main( ) FILE *fp; long coun

10、t=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的內(nèi)容輸出到屏幕上并復(fù)制到文件file2.c中。請?zhí)羁铡?# include <stdio.h> main( ) FILE (1) ; fp1= fopen(“file1.c”, ”r”); fp2= fopen(“file2.c”, ”w”)

11、; while(!feof(fp1) putchar(getc(fp1); (2) while(!feof(fp1) putc( (3) ); fclose(fp1); fclose(fp2); 6.以下程序段打開文件后,先利用 fseek函數(shù)將文件位置指針定位在文件末尾,然后調(diào)用ftell函數(shù)返回當前文件位置指針的具體位置,從而確定文件長度,請?zhí)羁?。FILE *myf; long f1;myf= fopen(“test.t”,“rb”); ;f1=ftell(myf);fclose(myf);printf(“%dn”,f1); 三、編程題1. 編寫一個程序,建立一個abc文本文件,向其中寫入

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

13、. 字節(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. 編寫一

14、個程序,建立一個abc文本文件,向其中寫入“this is a test”字符串,然后顯示該文件的內(nèi)容。#include <stdio.h>#include <string.h>#include<stdlib.h>main( )FILE *fp; char msg = "this is a test" char buf20; if(fp=fopen("abc","w+")=NULL) printf("不能建立abc文件n"); exit(0); fwrite(msg,strlen

15、(msg)+1,1,fp);fseek(fp,SEEK_SET,0);fread(buf,strlen(msg)+1,1,fp);printf("%sn",buf);fclose(fp);2.有5個學(xué)生,每個學(xué)生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號、姓名、三門課成績),計算出平均成績,將原有數(shù)據(jù)和計算出的平均分數(shù)存放在磁盤文件stud中。 #include<stdio.h> struct student char num10; char name8; int score3; float ave; stu5; main() int i,j,sum; FI

16、LE *fp; for(i=0;i<5;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;j<3;j+) printf("score %d :",j+1); scanf("%d",&stui.scorej);

17、 sum+=stui.scorej; stui.ave=sum/3.0; fp=fopen("stud","w"); for(i=0;i<5;i+) if(fwrite(&stui,sizeof(struct student),1,fp)!=1) printf("File write errorn"); fclose(fp); fp=fopen("stud","r"); for(i=0;i<5;i+) fread(&stui,sizeof(struct student

18、),1,fp); printf("%s,%s,%d,%d,%d,%6.2fn",stui.num,,stui.score0, stui.score1, stui.score2 ,stui.ave); 3.將上題stud文件中的學(xué)生數(shù)據(jù)按平均分進行排序處理,并將已排序的學(xué)生數(shù)據(jù)存入一個新文件stu-sort中。 #include <stdio.h> #include<stdlib.h>#define N 10 struct student char num10; char name8; int score3; float ave; s

19、tN,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=0;fread(&sti,sizeof(struct student),1,fp)!=0;i+) printf("n%8s%8s",sti.num,); for(j=0;j<

20、;3;j+) printf("%8d",sti.scorej); printf("%10.f",sti.ave); fclose(fp); n=i; for(i=0;i<n;i+) for(j=i+1;j<n;j+) if(sti.ave<stj.ave) temp=sti; sti=stj; stj=temp; printf("nnow:"); fp=fopen("stu-sort","w"); for(i=0;i<n;i+) fwrite(&sti,sizeo

21、f(struct student),1,fp); printf("n%8s%8s",sti.num,); for(j=0;j<3;j+) printf("%8d",sti.scorej); printf("%10.2f",sti.ave); fclose(fp); 4.將上題以排序的學(xué)生成績文件進行插入處理。插入一個學(xué)生的3門課成績,程序先計算新插入學(xué)生的平均成績,然后將它按平均成績高低順序插入,插入后建立一個新文件。 #include <stdio.h> #include<stdlib.h&g

22、t;struct student char num10; char name8; int score3; float ave; 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.

23、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); 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;j<3;j+) pr

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論