




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
7.5FileAccessThegeneralstepsforafileoperation: 1Openthefile 2Dooperationsonthefilebyfilepointer. 3CloseafileInClanguage,filesareaccessedbyfilepointers,whicharedeclaredas: FILE*fp;Duringthefirststep,wecanuseafopentomakefppointstothefilewewanttoaccess. fp=fopen(“data”,“r”); or if((fp=fopen(“data”,”r”))!=NULL)7.5FileAccess
FILE*fopen(char*filename,char*mode); intfclose(FILE*fp);1Thefilenamecanincludefullpath,justlike: fp=fopen(“c:\\system\\t.dat”,”r”);2Themodecanbe “r”:readonly “w”:writeonly “a”:appendonly Ifthesystemdistinguishbetweentextandbinaryfiles,forthelatter,a“b”mustbeappendedtothemodestring. fp=fopen(“t.dat”,”rb”);Whenoperationsfinished,thefileshouldbeclosed.fclose(fp);7.5FileAccess
Functionsusedtoaccessafile:1Readdatafromafile: intgetc(FILE*fp); intfscanf(FILE*fp,char*format,…); intfgets(char*string,intn,FILE*fp); intfread(charbuffer,intsize,intcount,FILE*fp);2Writedataintafile: intputc(intc,FILE*fp); intfprintf(FILE*fp,char*format,…); intfputs(char*string,intn,FILE*fo); intfwrite(char*buffer,intsize,intcount,FILE*fp)3filelocate fseek(FILE*fp,longoff,intstart);7.5FileAccesseg1:filecopy #include<stdio.h> main(intargc,char*argv[]) { FILE*fp1,*fp2; if(argc!=3) { printf(“Usage:copyf1f2\n”); exit(1); } if((fp1=fopen(argv[1],”r”))==NULL||((fp2=fopen(argv[2],”w”))==NULL) { printf(“Can’topenfile!\n”); exit(2); } while(putc(getc(fp1),fp2)!=EOF) ; fclose(fp1); fclose(fp2); }7.5FileAccesseg2:readfromacharacterfile,changelowertoupper. #include<stdio.h> main(intargc,char*argv[]) { FILE*fp1,*fp2; charc; if(argc!=3) exit(1);
if((fp1==fopen(*++argv,”r”))==NULL||((fp2=fopen(*++argv,”w”)=NULL) exit(2); while((c=fgetc(fp1))!=EOF) fputc(c+’A’-’a’,fp2); fclose(fp1); fclose(fp2); }7.5FileAccesseg2:concatenatefiles#include<stdio.h>main(intargc,char*argv[]){ FILE*fp; if(argc==1) filecopy(stdin,stdout); else while(--argc>0) if((fp=fopen(*++argv,”r”))==NULL){ printf(“cat:can’topen%s\n”,*argv); return1; }else{ filecopy(fp,stdout); fclose(fp); } return0;}7.5FileAccessvoidfilecopy(FILE*ifp,FILE*ofp){intc;while((c=getc(ifp))!=EOF) putc(c,ofp);}7.6LineInputandOutputchar*fgets(char*line,intmaxline,FILE*fp) fgetsreadnextinputline(includingthenewline)fromfilefpintocharacterarrayline,atmostmaxline-1characterswillberead Theresultlineisterminatedwith‘\0’;intfputs(char*line,FILE*fp) fputswritesastringtoafile(neednotcontainanewline)7.6LineInputandOutputchar*fgets(char*s,intn,FILE*iop){ registerintc; registerchar*cs; cs=s; while(--n>0&&(c=getc(iop))!=EOF) if((*cs++=c)==‘\n’) break; *cs=‘\0’; return(c==EOF&&CS==S)?NULL:s;}intfputs(char*s,FILE*iop){ intc; while(c=*s++) putc(c,iop);
returnferror(ip)?EOF:0;} Datafile問題1:讀入n個整數(比如n=10),將其從小到大排序后,存放在一個數據文件中。分別考慮該數據文件是文本形式和二進制形式兩種不同情況。問題2:從一個數據文件中讀入n個整數(比如n=10),將其從小到大排序后輸出,或者存放在另一個數據文件中。分別考慮該數據文件是文本形式和二進制形式兩種不同情況。voidsort(int*list,intn){
inti,j,next;
for(i=1;i<n;i++){ next=list[i]; for(j=i-1;j>=0&&next<list[j];j--) list[j+1]=list[j]; list[j+1]=next; }}main(){
intinput[10],i; FILE*fpin,*fpout; if((fpin=fopen("in.dat","r"))==NULL){ printf(“Cannotopenfilein.dat\n”);
exit(-1); } for(i=0;i<10;i++)
fscanf(fpin,"%d",&input[i]); /* fread(input,sizeof(int),10,fpin);*/ sort(input,10); if((fpout=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025跨國企業專有技術轉讓合同范本
- 2025設備采購合同機務高壓風機
- 船舶融資租賃合同
- 2025年共同經營合同協議書
- 《2025財務專項貸款合同》
- 購買足球捐款協議書
- 不良債權處置協議書
- 2025年03月廣西防城港市港口區審計局面向社會公開招聘1人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 2025年03月南昌高新區某單位服務外包人員公開招聘4人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 四川省巴中市達標名校2025年初三下期末模擬聯考英語試題含答案
- DB52T 1458-2019 貴州省森林人家建設標準
- 第47屆世界技能大賽制造團隊挑戰賽項目江蘇省選拔賽樣題(綜合制造專業方向)
- 【S鎮35kV變電站一次系統設計(論文)14000字】
- 江蘇省蘇州市(2024年-2025年小學四年級語文)人教版期中考試((上下)學期)試卷及答案
- 2024年6月廣東深圳市事業單位面試題及參考答案
- GB 44496-2024汽車軟件升級通用技術要求
- 第九單元 文人情致 課件高一音樂人音版(2019)必修 音樂鑒賞
- 全過程工程咨詢投標方案(技術方案)
- 關于學生假期(寒暑假)安排的調查問卷
- 缺血性腦卒中的護理
- 中國歷史-Chinese History (中英文)
評論
0/150
提交評論