C程序 語(yǔ)言文件_第1頁(yè)
C程序 語(yǔ)言文件_第2頁(yè)
C程序 語(yǔ)言文件_第3頁(yè)
C程序 語(yǔ)言文件_第4頁(yè)
C程序 語(yǔ)言文件_第5頁(yè)
已閱讀5頁(yè),還剩81頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、Intelligent Information Processing Lab., Dept.of Computer Sci. /* 定義文件指針 */ fp=fopen() ; /* 打開(kāi)文件,fp指向該文件控制信息 */ /* 文件讀寫(xiě) */ flocse(fp) ; /* 關(guān)閉文件,結(jié)束對(duì)指定文件的存取操作 */,Intelligent Information Processing Lab., Dept.of Computer Sci. filename: 字符串,需要使用的文件名 mode: 字符串,文件使用方式 (see:【p333】Tab.13-1) 閱讀【pp333-334】(1)

2、-(8),File Opening ,Intelligent Information Processing Lab., Dept.of Computer Sci. discard previous contents if any 產(chǎn)生新文件 ”a”: append; open or create text file for writing at end of file 如果文件不存在則產(chǎn)生新文件,追加數(shù)據(jù),File Opening discard previous contents if any 產(chǎn)生新文件 ”a+”: append; open or create text file for

3、update, writing at end 如果文件不存在則產(chǎn)生新文件,寫(xiě)時(shí)追加數(shù)據(jù),File Opening ,Intelligent Information Processing Lab., Dept.of Computer Sci. , 可否按二進(jìn)制方式打開(kāi)?,Intelligent Information Processing Lab., Dept.of Computer Sci. char fname81=d:mytext.txt; FILE *fp; if ( (fp=fopen(fname, w)=NULL ) puts(n文件打開(kāi)失敗!); return ; for (ch=

4、A; ch=Z; ch+) if ( fputc(ch, fp)!=ch ) puts(n文件寫(xiě)入錯(cuò)誤!); return; fclose(fp); puts(n文件寫(xiě)入完畢!); return; ,打開(kāi)文件,寫(xiě)入一個(gè)字符,關(guān)閉文件,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,超出文件末尾字節(jié)而繼續(xù)讀時(shí)返回非0值(讀無(wú)效),否則返回0(讀有效),Intelligent Informat

5、ion Processing Lab., Dept.of Computer Sci. ,文件1,文件2,in,out,copy,已超出文件最后一個(gè)字節(jié),但此時(shí)feof(in)=0,因此!feof(in)=1,將多復(fù)制1個(gè)垃圾字節(jié),應(yīng)該采用【pp335】程序段的思路!,Intelligent Information Processing Lab., Dept.of Computer Sci. char ch; char infile20=d:mytext.txt; char outfile20=d:newtext.txt; unsigned long int byte=0; /* 復(fù)制的字節(jié)數(shù)

6、*/ if ( (in=fopen(infile, rb)=NULL ) puts(“n不能打開(kāi)源文件!); return; if ( (out=fopen(outfile, wb)=NULL ) puts(n不能打開(kāi)目標(biāo)文件!); return; ,while ( ch=fgetc(in), feof(in)=0 ) fputc(ch, out); byte+; fclose(in); fclose(out); printf(n%lu個(gè)字節(jié)復(fù)制完畢, byte); return; ,教材【pp337】例13.2中定義了ch卻沒(méi)使用?。浚??,逗號(hào)表達(dá)式, 值為feof(in)=0的值,兩個(gè)文件

7、分別都要關(guān)閉,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,程序數(shù)據(jù)區(qū),磁盤(pán),緩沖區(qū)滿或文件關(guān)閉時(shí),文件,fp,字節(jié)數(shù):size,buffer,塊數(shù): count,FILE類型 信息區(qū)域,文件打開(kāi),001101011100,Intelligent Informa

8、tion Processing Lab., Dept.of Computer Sci. 功能: 輸入n名學(xué)生數(shù)據(jù)并存放到st指向的(共享存儲(chǔ)區(qū))n個(gè)結(jié)構(gòu)體數(shù)據(jù)區(qū)域中 save函數(shù)對(duì)fwrite()的包裝 原型: unsigned int save(SCORE *st, int n, char *fname); 功能: 把st所指的數(shù)據(jù)區(qū)內(nèi)的數(shù)據(jù)全部寫(xiě)入文件(fname指向文件名字符串),一個(gè)結(jié)構(gòu)體對(duì)象為數(shù)據(jù)塊,n為數(shù)據(jù)塊個(gè)數(shù);返回文件字節(jié)數(shù),File Reading int sub3 ; ; typedef struct score SCORE ; void input(SCORE *, in

9、t); unsigned int save(SCORE *, int, char *); void main() SCORE studentN ; /* 開(kāi)辟內(nèi)存空間 */ char file20=”d:score.fil” ; unsigned int byte ; input(student, N) ; /* 輸入數(shù)據(jù)*/ byte=save(student, N, file); /* 保存到文件 */ if ( byte!=N*sizeof(SCORE) printf(”n數(shù)據(jù)保存失敗”); else printf(”n數(shù)據(jù)保存成功, 文件字節(jié)數(shù):%u”, byte); ,Intelli

10、gent Information Processing Lab., Dept.of Computer Sci. for (p=st; pno, p-sub, p-sub+1, p-sub+2) ; return ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. FILE *fout ; if ( (fout=fopen(fname, ”wb”)=NULL ) printf(”n文件%s創(chuàng)建失敗!”, fname) ; return 0; count=fwrite(st, sizeof(SCORE), n, fou

11、t) ; fclose(fout); return count*sizeof(SCORE) ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,程序數(shù)據(jù)區(qū),磁盤(pán),001101011100,緩沖區(qū)空或沒(méi)有所需數(shù)據(jù)時(shí),文件,fp,字節(jié)數(shù):size,buffer,塊數(shù)

12、: count,FILE類型 信息區(qū)域,文件打開(kāi),Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 從文件(fname指向文件名字符串)讀入數(shù)據(jù),以一個(gè)結(jié)構(gòu)體對(duì)象為數(shù)據(jù)塊,以n為數(shù)據(jù)塊個(gè)數(shù),存放到st所指的數(shù)據(jù)區(qū)內(nèi)(構(gòu)成有n個(gè)元素的結(jié)構(gòu)體數(shù)組);返回讀入的字節(jié)數(shù) output函數(shù) 原型: void input(SCORE *st, int n); 功能: 對(duì)數(shù)組中的n名學(xué)生成績(jī)進(jìn)行平均值計(jì)算,并顯示,File Reading int sub3 ; ; typedef struct score SCORE ; v

13、oid output(SCORE *, int); unsigned int load(SCORE *, int, char *); void main() SCORE studentN ; /* 開(kāi)辟內(nèi)存空間 */ char file20=”d:score.fil” ; unsigned int byte ; byte=load(student, N, file); /* 讀文件 */ if ( byte!=N*sizeof(SCORE) printf(”n數(shù)據(jù)調(diào)入失敗”); else printf(”n數(shù)據(jù)調(diào)入成功, 文件字節(jié)數(shù):%u”, byte); output(student, N)

14、 ; /* 統(tǒng)計(jì)并輸出數(shù)據(jù)*/ ,為讀入數(shù)據(jù)準(zhǔn)備“場(chǎng)地”,Intelligent Information Processing Lab., Dept.of Computer Sci. for (p=st; psub+i); printf(”n學(xué)號(hào):%s 平均成成績(jī):%f”, p-no, sco/3.0); return ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. FILE *fin ; if ( (fin=fopen(fname, ”rb”)=NULL ) printf(”n文件%s打開(kāi)失敗!”, fnam

15、e) ; return 0; count=fread(st, sizeof(SCORE), n, fin) ; return count*sizeof(SCORE) ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,while (ch=fgetc(fp1), !feof(fp1) putchar(ch);,getc是stdio.h中定義的宏,可認(rèn)為功能與fgetc相同,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 把文件指針

16、fp指定的文件當(dāng)前數(shù)據(jù)讀寫(xiě)位置以base為基準(zhǔn)相距offset字節(jié)的位置;定位失敗時(shí)返回值為非0,成功時(shí)為0 offset的取值: 長(zhǎng)整型,常量后加L,正(負(fù))數(shù)表示向文件末尾(開(kāi)頭)方向移動(dòng) base的取值: SEEK_SET、SEEK_CUR、SEEK_END(這些符號(hào)常量在stdio.h中定義為0、1、2) 閱讀【pp346】例13.5,File Positioning,60,rewind(fp) fseek(fp, 0L, SEEK_SET),0101010101010111101010110110101101111101,SEEK_SET,SEEK_END,SEEK_CUR,offs

17、et = -2,offset = +2,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 返回文件指針fp指定的文件當(dāng)前數(shù)據(jù)讀寫(xiě)位置,檢測(cè)失敗時(shí)返回-1L,File Positioning,61,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 返回指定文件(由fname指定文件名)的字節(jié)數(shù)(磁盤(pán)占有量) 算法 以只讀方式打開(kāi)文件 數(shù)據(jù)讀寫(xiě)位置定位到文件末尾 返回當(dāng)前ftell()的值,File Positioning,62,0

18、101010101010111101010110110101101111101,SEEK_END,ftell(fp)=5,位置指針,Intelligent Information Processing Lab., Dept.of Computer Sci. long int byte ; if ( (fp=fopen(fname, ”rb”)=NULL ) return -1L; if ( fseek(fp, 0L, SEEK_END)!=0 ) return -1L; byte=ftell(fp); fclose(fp) ; return byte ; ,可否寫(xiě)成return ftell(f

19、p); ?,Intelligent Information Processing Lab., Dept.of Computer Sci. void main() long int size ; if ( (size=fsize(”d:mytext.txt”)=-1L ) puts(”n文件長(zhǎng)度檢測(cè)失敗”); else printf(”n文件長(zhǎng)度為%ld字節(jié)”, size); ,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能:如果最近一次對(duì)fp指定文件的I/O操作發(fā)生了錯(cuò)誤,則返回非0值,否則返回0,Error Handling,66,Intelligent Information Processi

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論