




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第12章 文件【練習(xí)12-1】讀出例12-1學(xué)生成績(jī)文件f12-1.txt內(nèi)容,輸出最高分和最低分及相應(yīng)的學(xué)號(hào)和姓名。解答:#include<stdio.h>#include<stdlib.h>struct studentlong num;char stname20;int score;int main(void) FILE *fp; int i,max,min,j=0,k=0; struct student students5; if(fp=fopen("f12-1.txt","r")=NULL) printf("Fi
2、le open error!n"); exit(0);fscanf(fp,"%ld%s%d",&students0.num,students0.stname,&students0.score);max=min=students0.score; for(i=1;i<=4;i+) fscanf(fp,"%ld%s%d",&studentsi.num,studentsi.stname,&studentsi.score); if(max<studentsi.score) max=studentsi.score
3、; j=i; if(min>studentsi.score) min=studentsi.score; k=i; printf("Max score: %d,num:%d,name:%sn",studentsj.score,studentsj.num,&studentsj.stname); printf("Min score: %d,num:%d,name:%sn",studentsk.score,studentsk.num,&studentsk.stname); if(fclose(fp) printf("Can not
4、 close the file!n"); exit(0); return 0;【練習(xí)12-2】請(qǐng)使用例8-9答電碼加密函數(shù)對(duì)民嗎字符串進(jìn)行加密,改寫例12-2。解答:#include<stdio.h>#include<string.h>#include<stdlib.h>struct sysuser char username20; char password8;void encrypt(char *pwd); int main(void) FILE *fp; int i; struct sysuser su; if(fp=fopen("
5、f12-2.txt","w")=NULL) printf("File open error!n"); exit(0); for(i=1;i<=5;i+) printf("Enter %dth sysuser (name password):",i); scanf("%s%s",su.username,su.password); encrypt(su.password); fprintf(fp,"%s %sn",su.username,su.password); if(fclose
6、(fp) printf("Can not close the file!n"); exit(0); return 0;void encrypt(char *pwd)int i; for(i=0;i<strlen(pwd);i+) if(pwdi='z') pwdi='a' else pwdi+=1;【練習(xí)12-3】例12-3中為什么在執(zhí)行fputc(ch,fp2)前要判斷ch的值是否等于EOF?改寫例12-3的程序,在復(fù)制用戶信息文件后,再統(tǒng)計(jì)被復(fù)制文件中字符的數(shù)量。解答:文件結(jié)束符EOF是一個(gè)值為-1的常量,讀文件時(shí)可用來(lái)判斷從文件中
7、讀入的字符是否為EOF來(lái)決定循環(huán)是否繼續(xù)。#include<stdio.h>#include<stdlib.h>int main(void) FILE *fp1,*fp2; char ch; int count=0; if(fp1=fopen("f12-2.txt","r")=NULL) printf("File open error!n"); exit(0); if(fp2=fopen("f12-3.txt","w")=NULL) printf("File o
8、pen error!n"); exit(0); while(!feof(fp1) ch=fgetc(fp1); if(ch!=EOF) fputc(ch,fp2);count+; if(fclose(fp1) printf("Can not close the file!n"); exit(0); if(fclose(fp2) printf("Can not close the file!n"); exit(0); printf("f12-2中字符數(shù)量為:%d",count); return 0; 【練習(xí)12-4】字母轉(zhuǎn)換并
9、統(tǒng)計(jì)行數(shù): 讀取一個(gè)指定的文本文件,顯示在屏幕上,如果有大寫字母,則改成小寫字母再輸出,并根據(jù)回車符統(tǒng)計(jì)行數(shù)。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void)char ch;int countline=0;FILE *fp;if(fp=fopen("練習(xí)12-4.txt","r")=NULL)printf("Not open!");exit(0);while(!feof(fp)ch=fgetc(fp);if(ch!=EOF) if(ch&g
10、t;='A'&&ch<='Z') printf("%c",ch-'A'+'a'); else printf("%c",ch);if(ch='n') countline+;printf("n");printf("file's line is:%d.",countline+1);if(fclose(fp)printf("Can not close!");exit(0);return 0;【練
11、習(xí)12-5】寫字符并驗(yàn)證: 從鍵盤輸入一行字符,寫入到文件f3.txt中,并重新讀出,最終在屏幕上顯示驗(yàn)證。程序輸入以讀到回車符“n”為結(jié)束,讀文件時(shí)要用EOF來(lái)控制循環(huán)。試編寫相應(yīng)程序。解答:#include<stdio.h> #include<stdlib.h>int main(void) FILE *fp; char ch; if(fp=fopen("f3.txt","w+")=NULL) printf("can not open file!"); exit(0); printf("Input
12、the string:n"); ch=getchar(); while(ch!='n') fputc(ch,fp); ch=getchar(); rewind(fp); while(!feof(fp) ch=fgetc(fp); if(ch!=EOF) putchar(ch); printf("n"); if(fclose(fp) printf("can not close file!n"); exit(0);return 0;【練習(xí)12-6】實(shí)數(shù)取整寫入文件: 文件f1.txt中有若干個(gè)實(shí)數(shù),請(qǐng)分別讀出,將每個(gè)實(shí)數(shù)按四舍五入取
13、整后存入文件f2.txt中。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void) FILE *fp1,*fp2; double a; if(fp1=fopen("f1.txt","r")=NULL) printf("File open error!n"); exit(0); if(fp2=fopen("f2.txt","w")=NULL) printf("File open error!n&qu
14、ot;); exit(0); while(!feof(fp1) fscanf(fp1,"%lf",&a); fprintf(fp2,"%.0f ",a); if(fclose(fp1) printf("Can not close the file!n"); exit(0); if(fclose(fp2) printf("Can not close the file!n"); exit(0); return 0;【練習(xí)12-7】修改例12-6,增加修改資金賬戶的功能。輸入一個(gè)記錄ID,如果文件中已存在該記錄,
15、則輸入新的記錄信息并更新資金賬戶文件中相應(yīng)記錄的信息。要求定義和調(diào)用函數(shù)UpdateLog(),其功能是修改資金賬戶記錄。解答:#include<stdio.h>#include<stdlib.h>long size;struct LogData long logid; char logdate11; char 1ognote15; double charge; double balance; ;int inputchoice() int mychoice; printf("nEnter your choice: n"); printf("
16、1-Add a new cash LOG.n2-List All Cash LOG.n");printf("3-Query Last Cash LoG.n0-End program.n"); scanf("%d",&mychoice); return mychoice;long getLogcount(FILE *cfptr) long begin,end,logcount; fseek(cfptr,OL,SEEK_SET); begin=ftell(cfptr); fseek(cfptr,size,SEEK_END); end=fte
17、ll(cfptr); logcount=(end-begin)/size-1; return logcount;/*列出所有收支流水賬*/void ListAllLog(FILE *cfptr) struct LogData log; fseek(cfptr,OL,SEEK_SET); fread(&log,size,1,cfptr); printf("logid logdate lognote charge balance n"); while(!feof(cfptr) printf("%6ld%-11s%-15%10.2lf%10.2lfn"
18、,log.logid,log.logdate,log.lognote,log.charge,log.balance); fread(&log,size,1,cfptr); ;/*查詢顯示最后一條記錄*/void QueryLastLog(FILE *cfptr) struct LogData log; long logcount; logcount=getlogcount(cfptr); if(1ogcount>0) fseek(cfptr,size*(logcount-1),SEEK_SET); fread(&log,size,1,cfptr); printf(&quo
19、t;The last log is:n"); printf("logid:%-6ldnlogdate:%-11snlognote:%-15sn",log.logid,log.logdate,log.lognote); printf("charge:%-10.2lfnbalance:-10.2lfn",log.charge,1og.balance); else printf("no logs in file!n");/*添加新記錄*/void AddNewLog(FILE *cfptr) struct LogData log,
20、lastlog; long logcount; printf("Input logdate (format: 2006-01-01):"); scanf("%s",log.logdate); printf("Input lognote:"); scanf("%s",log.lognote); printf("Input Charge: Income+and epend-:"); scanf("%lf",&log.charge); logcount=getLogcoun
21、t(cfptr); if(logcount>0) fseek(cfptr,size*(logcount-1),SEEK_SET);fread(&lastlog,size,1,cfptr) log.logid=lastlog.1ogid+1; log.balance=log.charge+lastlog.balance; else log.logid=1; log.balance=log.charge; rewind(cfptr);ogid=last-taraetlastlog; printf("logid=%ldn",log.logid); fwirte(&a
22、mp;log,sizeof(struct LogData),1,cfptr);/*修改資金賬戶*/void UpdateLog(FILE *cfptr)FILE *fpout; struct LogData user; char date11; char note15; double charge; double balance; int choice,ID; cfptr=fileopen("r"); if(fpout=fopen("cash.dat","w")=NULL) printf("can not open the
23、file !n"); exit(0); printf("Enter LogID:");scanf("%d",&ID);while(!feof(cfptr) fread(&user,sizeof(struct LogData),1,cfptr); if(strcmp(user.logid,ID)=0) printf("請(qǐng)輸入修改信息:n"); printf("Date:"); scanf("%s",date); strcpy(user.logdate,date); pri
24、ntf("Note:"); scanf("%s",note); strcpy(user.lognote,note); printf("Charge:"); user.charge=charge; printf("Balance:"); scanf("%s",&balance); user.balance=balance; fwrite(&user,sizeof(struct LogData),1,fpout);else fwrite(&user,sizeof(struct
25、 LogData),1,fpout); if(fclose(cfptr)printf("can not close file!n");exit(0); if(fclose(fpout)printf("can not close file!n");exit(0); unlink("cashbox.dat");/remove("cashbox.dat");都是刪除文件的函數(shù) rename("cash.dat.dat","cashbox.dat");/*打開(kāi)文件*/FILE *op
26、enfile(char *openmode) FILE *fp; if(fp=fopen("cashbox.dat",openmode)=NULL) printf("can not open file cashbox.dat!n"); exit(0); return (fp);int main(void) FILE *fp; int choice; size=sizeof(struct LogData); while(choice=inputchoice()!=0) switch(choice) case 1: fp=openfile("ab+
27、"); AddNewLog(fp); break; /*列出所有的收入支出情況*/ case 2: fp=openfile("rb"); ListAllLog(fp); break; /*查詢最后記錄及余額*/ case 3: fp=openfile("rb"); QueryLastLog(fp); break; case 4: fp=openfile("rb"); UpdateLog(fp); break; default: printf("Input Error."); break; if(fclos
28、e(fp) printf("Can not close the file!n"); exit(0); return 0;習(xí)題121、 選擇題1 .以下語(yǔ)句將輸出_B_。#include<stdio.h>printf ("%d %d %d",NULL,0',EOF);A.0 0 1 B.0 0 -1 C.NULL EOF D.1 0 EOF2.如果二進(jìn)制文件a.dat 已經(jīng)存在,現(xiàn)在要寫入全新數(shù)據(jù),應(yīng)以_B_方式打開(kāi)。A."w" B."wb" C."w+" D.“wb+&quo
29、t;3.定義”FILE *fp;",則文件指針fp 指向的是_D_。A.文件在磁盤上的讀寫位置 B文件在級(jí)沖區(qū)上的讀寫位置C.整個(gè)磁盤文件 D.文件類型結(jié)構(gòu)4.緩沖文件系統(tǒng)的文件緩沖區(qū)位于_C_。A.磁盤緩沖區(qū)中 B.磁盤文件中C.內(nèi)存數(shù)據(jù)區(qū)中 D.程序文件中5.使文件指針重新定位到文件讀寫的首地址的函數(shù)是_C_。A.ftell ( ) B.fseek ( ) C.rewind ( ) D.ferror( )二、填空題1.函數(shù)fopen ( )的返回值是_指向文件緩沖區(qū)的首地址的文件結(jié)構(gòu)類型指針_。2.文件的三大特征是_名稱_、_大小_和_類型_。3.緩沖文件系統(tǒng)與非緩沖文件系統(tǒng)的不
30、同點(diǎn)在于_系統(tǒng)是否為文件自動(dòng)分配一塊文件緩沖區(qū)(內(nèi)存單元)_。4.只能向指定文件寫入一個(gè)字符的函數(shù)是_fputc()函數(shù)_。5.判斷文件指針是否已經(jīng)到了文件尾部的函數(shù)是_feof()函數(shù)_。6.閱讀程序,以下程序完成的功能是_文件infile內(nèi)容復(fù)制到文件_。#include <stdio.h>int main (void) char infile 10,outfile 10;FILE * fpa,* fpb;gets (infile);gets (outfile);fpa=fopen (infile,"r" );fpb=fopen (outfile,"
31、; w" );while (! feof (fpa) )fputc(fgetc(fpa),fpb);fclose (fpa) ;fclose (fpb) ;return 0;三、程序設(shè)計(jì)題1.統(tǒng)計(jì)文本文件中各類字符個(gè)數(shù): 分別統(tǒng)計(jì)一個(gè)文本文件中字母、數(shù)字及其他字符的個(gè)數(shù)。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void) FILE *fp; char ch; int letter=0,digiter=0,other=0; if(fp=fopen("12-3.1.txt",
32、"r")=NULL) printf("File open error!n"); exit(0); while(ch=fgetc(fp)!=EOF) if(ch>='A'&&ch<='Z'|ch>='a'&&ch<='z') letter+; else if(ch>='0'&&ch<='9') digiter+; else other+; printf("letter=
33、%d,digiter=%d,other=%d",letter,digiter,other); if(fclose(fp) printf("Can not close the file!n"); exit(0); return 0; 2.將實(shí)數(shù)寫入文件: 從鍵盤輸人若干實(shí)數(shù)(以特殊數(shù)值-1結(jié)束),分別寫到一個(gè)文本文件中。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void)FILE *fp;int number;if(fp=fopen("12-3.2.txt"
34、;,"w")=NULL)printf("can not open file!n");exit(0);printf("Input number:n");scanf("%d",&number);while(number!=-1)fprintf(fp,"%d",number);scanf("%d",&number); if(fclose(fp) printf("Can not close the file!n"); exit(0); return
35、 0;3.比較兩個(gè)文本文件是否相等: 比較兩個(gè)文本文件的內(nèi)容是否相同,并輸出兩個(gè)文件中第一次出現(xiàn)不同字符內(nèi)容的行號(hào)及列值。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void)FILE *fp1,*fp2;int i=1,j=1;char ch1,ch2;if(fp1=fopen("12-3.3.1.txt","r")=NULL)printf("can not open file!n");exit(0);if(fp2=fopen("12
36、-3.3.2.txt","r")=NULL)printf("can not open file!n");exit(0);ch1=fgetc(fp1);ch2=fgetc(fp2);while(ch1!=EOF&&ch2!=EOF)if(ch1!=ch2) break;else if(ch1!='n')j+;ch1=fgetc(fp1);ch2=fgetc(fp2);elsei+;j=1;ch1=fgetc(fp1);ch2=fgetc(fp2); printf("首次不同的字符在第%d行、第%d列。n&
37、quot;,i,j);if(fclose(fp1)printf("can not close file!n");exit(0);if(fclose(fp2)printf("can not close file!n");exit(0);4.將文件中的數(shù)據(jù)求和并寫入文本文件尾: 文件Int_Data.dat 中存放了若干整數(shù),將文件中所有數(shù)據(jù)相加,并把累加和寫入該文件的最后。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>int main(void)FILE *fp;int x,sum=0;
38、if(fp=fopen("Int_Date.dat","ab+")=NULL)printf("can not open file!");exit(0);while(!feof(fp)fscanf(fp,"%d",&x);sum+=x;fprintf(fp," %d",sum);if(fclose(fp)printf("can not close the file!n");exit(0);return 0;5.輸出含for的行: 將文本文件test.txt 中所有包含字
39、符串“for”的行輸出。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib.h>#include<string.h>char s999;int main(void) FILE* fp; int i; if(fp=fopen("test.txt","r")=NULL)printf("can not open file!n");exit(0);while(!feof(fp) fgets(s,999,fp); if(strlen(s)>3) for(i=0;i&l
40、t;strlen(s)-2;i+) if(si='f'&&si+1='o'&&si+2='r') printf("%s",s); if(fclose(fp)printf("can not close file!n");exit(0); return 0;6.刪除文件中的注釋: 將C語(yǔ)言源程序(hello.c) 文件中的所有注釋去掉后存入另一個(gè)文件(new_hello.c)。試編寫相應(yīng)程序。解答:#include<stdio.h>#include<stdlib
41、.h>int main(void)FILE *fp1,*fp2;char ch,ch1,ch2,s99;if(fp1=fopen("hello.c","r")=NULL)printf("can not open file!n");exit(0);if(fp2=fopen("new_hello.c","w")=NULL)printf("can not open file!n");exit(0); while(!feof(fp1) ch=fgetc(fp1); if(ch=
42、'/') if(ch1=fgetc(fp1)='*') while(fgetc(fp1)!='*'&&(ch1=fgetc(fp1)!='/') fseek(fp1,-sizeof(ch1),1); else if(ch1='/') ch='0' for(;ch1!='n'ch1=fgetc(fp1) ch1='0' else if(ch!=EOF) fputc(ch,fp2);if(fclose(fp2)printf("can not cl
43、ose file!n");exit(0); return 0;7.(選做) 賬戶余額管理: 創(chuàng)建一個(gè)隨機(jī)文件,用來(lái)存儲(chǔ)銀行賬戶和余額信息,程序要求能夠查詢某個(gè)賬戶的余額,當(dāng)客戶發(fā)生交易額時(shí)(正表示存入,負(fù)表示取出)能夠更新余額。賬戶信息包括賬號(hào)、賬號(hào)名和余額三個(gè)數(shù)據(jù)項(xiàng)。試編寫相應(yīng)程序。文件部分內(nèi)容如下:AcctNo AcctName Balance 1 zhangsan 1000.00 2 lisi 1300.00 3 wangwu -100.00 解答:#include <stdio.h>#include<string.h>#include <stdl
44、ib.h>long size;struct account char no10; char acctname50; double balance;FILE *openfile(char *openmode) FILE *fp; if(fp=fopen("accout.dat",openmode)=NULL) printf("Can not open the file!"); exit(0); return fp;double userbalance(FILE *fp,char *name);void pay(FILE *fp,char *name,double count);int main() FILE *fp; int choice; char name50; double balance; double count; printf("請(qǐng)輸入選擇類型:n"); printf("1.查賬戶余額n"); printf("2.賬戶交易n"); printf("
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 單位員工招聘合同范本
- 獸醫(yī)聘用勞務(wù)合同范本
- 光催化課題申報(bào)書
- 會(huì)所物資出售合同范本
- 廚具采買合同范本寫
- 吊裝合同范例簡(jiǎn)易版本
- 醫(yī)院雇傭合同范本
- 企業(yè)各類合同范本
- 吊車及場(chǎng)地合作合同范本
- 合同范例結(jié)構(gòu)特征有
- 玉米收購(gòu)可行性分析報(bào)告
- 最全醫(yī)院應(yīng)急預(yù)案匯編目錄
- 駕駛員心理健康教育培訓(xùn)課件
- 別墅的價(jià)格評(píng)估報(bào)告
- 滬科版七年級(jí)數(shù)學(xué)下冊(cè) 第六章 實(shí)數(shù) 單元測(cè)試卷
- 無(wú)痛胃腸鏡的護(hù)理查房
- 農(nóng)村生活污水檢測(cè)服務(wù)方案
- 中華人民共和國(guó)傳染病防治法-李碩娟 陳桂云
- 熱力管網(wǎng)運(yùn)行工施工工序標(biāo)準(zhǔn)詳細(xì)流程培訓(xùn)
- 駕駛員心理健康與安全駕駛
- 基于強(qiáng)化學(xué)習(xí)的特征選擇技術(shù)
評(píng)論
0/150
提交評(píng)論