版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、VC讀寫函數(shù)詳解1當(dāng)前文件指針位置獲取函數(shù)long ftell( FILE *stream );參數(shù):stream 文件指針?lè)祷刂担寒?dāng)前文件指針的位置實(shí)例:#include FILE *stream;void main( void ) long position; char list100; if( (stream = fopen( ftell.c, rb ) != NULL ) /* Move the pointer by reading data: */ fread( list, sizeof( char ), 100, stream ); /* Get position after re
2、ad: */ position = ftell( stream ); printf( Position after trying to read 100 bytes: %ldn, position ); fclose( stream ); 輸出:Position after trying to read 100 bytes: 1002文件的打開(kāi)函數(shù)FILE *fopen( const char *filename, const char *mode );FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );參數(shù):filena
3、me 文件的名稱mode 打開(kāi)文件的模式返回值:成功的打開(kāi)文件的話返回 文件的指針 否則返回NULL表示打開(kāi)文件錯(cuò)誤注:r 只讀方式打開(kāi)w 以寫入方式打開(kāi)a 從文件的尾端寫入數(shù)據(jù),要是文件不存在則創(chuàng)建后寫入數(shù)據(jù)r+ 以讀寫方式打開(kāi)(文件必須存在)w+ 打開(kāi)一個(gè)可以讀寫的文件,如果該文件存在則覆蓋寫入a+ 打開(kāi)文件并追加寫入源于實(shí)例:#include FILE *stream, *stream2;void main( void ) int numclosed; /* Open for read (will fail if file data does not exist) */ if( (str
4、eam = fopen( data, r ) = NULL ) printf( The file data was not openedn ); else printf( The file data was openedn ); /* Open for write */ if( (stream2 = fopen( data2, w+ ) = NULL ) printf( The file data2 was not openedn ); else printf( The file data2 was openedn ); /* Close stream */ if( fclose( strea
5、m ) ) printf( The file data was not closedn ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( Number of files closed by _fcloseall: %un, numclosed );輸出:The file data was openedThe file data2 was openedNumber of files closed by _fcloseall: 13.文件讀取函數(shù)size_t fread( void *buffer, si
6、ze_t size, size_t count, FILE *stream )參數(shù): buffer 文件讀取緩沖區(qū) size 讀取數(shù)據(jù)的類型 count 讀取數(shù)據(jù)的個(gè)數(shù) stream 文件指針?lè)祷刂担簩?shí)際讀取的數(shù)據(jù)個(gè)數(shù)源于例子:FILE* fp;char* buffer=new char1024;long realLength=fread(buffer,sizeof(char),1024,fp);/處理過(guò)程/delete buffer;fclose(fp);4.文件的寫入函數(shù)size_t fwrite( const void *buffer, size_t size, size_t count
7、, FILE *stream );參數(shù): buffer 所要寫入文件的緩沖區(qū) size 寫入數(shù)據(jù)的類型 count 寫入數(shù)據(jù)的個(gè)數(shù) stream 文件指針?lè)祷刂担簩?shí)際寫入的數(shù)據(jù)個(gè)數(shù)源于實(shí)例:#include void main( void ) FILE *stream; char list30; int i, numread, numwritten; /* Open file in text mode: */ if( (stream = fopen( fread.out, w+t ) != NULL ) for ( i = 0; i 25; i+ ) listi = (char)(z - i)
8、; /* Write 25 characters to stream */ numwritten = fwrite( list, sizeof( char ), 25, stream ); printf( Wrote %d itemsn, numwritten ); fclose( stream ); else printf( Problem opening the filen ); if( (stream = fopen( fread.out, r+t ) != NULL ) /* Attempt to read in 25 characters */ numread = fread( li
9、st, sizeof( char ), 25, stream ); printf( Number of items read = %dn, numread ); printf( Contents of buffer = %.25sn, list ); fclose( stream ); else printf( File could not be openedn );輸出:Wrote 25 itemsNumber of items read = 25Contents of buffer = zyxwvutsrqponmlkjihgfedcb5.文件指針?biāo)阉骱瘮?shù)int fseek( FILE *
10、stream, long offset, int origin )參數(shù): stream 文件指針 offset 從當(dāng)前指針開(kāi)始的偏移量 origin 文件指針當(dāng)前的位置返回值:成功返回0 否則返回非零值注:指針移動(dòng)的時(shí)候是按字節(jié)移動(dòng)的,要是成功的話 文件的指針將指向當(dāng)前搜索到的位置origin 可以的取值在 stdio.h已經(jīng)定義如下:SEEK_CUR 當(dāng)前的文件指針SEEK_END 文件結(jié)束SEEK_SET 文件的開(kāi)始源于實(shí)例:#include void main( void ) FILE *stream; char line81; int result; stream = fopen( f
11、seek.out, w+ ); if( stream = NULL ) printf( The file fseek.out was not openedn ); else fprintf( stream, The fseek begins here: This is the file fseek.out.n ); result = fseek( stream, 23L, SEEK_SET); if( result ) printf( Fseek failed ); else printf( File pointer is set to middle of first line.n ); fg
12、ets( line, 80, stream ); printf( %s, line ); fclose( stream ); 輸出:File pointer is set to middle of first line.This is the file fseek.out.6.按固定的格式寫入數(shù)據(jù)函數(shù)int fprintf( FILE *stream, const char *format , argument .)int fwprintf( FILE *stream, const wchar_t *format , argument .)參數(shù):stream 文件指針format 按照一定的格
13、式argument 可選參數(shù)列表返回值:fprintf 返回實(shí)際寫入的字節(jié)數(shù).fwprintf返回實(shí)際寫入的wchar_t 的字節(jié)數(shù)源于實(shí)例:#include #include FILE *stream;void main( void ) int i = 10; double fp = 1.5; char s = this is a string; char c = n; stream = fopen( fprintf.out, w ); fprintf( stream, %s%c, s, c ); fprintf( stream, %dn, i ); fprintf( stream, %fn
14、, fp ); fclose( stream ); system( type fprintf.out );輸出:this is a string101.5000007按固定的格式讀入數(shù)據(jù)函數(shù)int fscanf( FILE *stream, const char *format , argument . )int fwscanf( FILE *stream, const wchar_t *format , argument . )參數(shù):stream 文件指針format 按照一定的格式argument 可選參數(shù)列表返回值:fscanf 返回實(shí)際讀入的字節(jié)數(shù).fwscanf返回實(shí)際讀入的wcha
15、r_t 的字節(jié)數(shù)如果返回值為 0 則說(shuō)明沒(méi)有被賦值如果有文件結(jié)束或是異常的IO錯(cuò)誤時(shí) 返回 EOF(宏定義)源于實(shí)例:#include FILE *stream;void main( void ) long l; float fp; char s81; char c; stream = fopen( fscanf.out, w+ ); if( stream = NULL ) printf( The file fscanf.out was not openedn ); else fprintf( stream, %s %ld %f%c, a-string, 65000, 3.14159, x )
16、; /* Set pointer to beginning of file: */ fseek( stream, 0L, SEEK_SET ); /* Read data back from file: */ fscanf( stream, %s, s ); fscanf( stream, %ld, &l ); fscanf( stream, %f, &fp ); fscanf( stream, %c, &c ); /* Output data read: */ printf( %sn, s ); printf( %ldn, l ); printf( %fn, fp ); printf( %c
17、n, c ); fclose( stream ); 輸出:a-string650003.141590X8文件指針的定位和獲取int fsetpos( FILE *stream, const fpos_t *pos );int fgetpos( FILE *stream, const fpos_t *pos );參數(shù):stream 目標(biāo)文件指針pos 文件指針的位置返回值:設(shè)置指針位置成功的話fsetpos返回0 否則 返回一個(gè)非零的數(shù)獲得指針位置成功的話fgetpos返回0 否則 返回一個(gè)非零的數(shù)源于實(shí)例:#include void main( void ) FILE *stream; fpo
18、s_t pos; char buffer20; if( (stream = fopen( fgetpos.c, rb ) = NULL ) printf( Trouble opening filen ); else /* Read some data and then check the position. */ fread( buffer, sizeof( char ), 10, stream ); if( fgetpos( stream, &pos ) != 0 ) printf( fgetpos error ); else fread( buffer, sizeof( char ), 1
19、0, stream ); printf( 10 bytes at byte %I64d: %.10sn, pos, buffer ); /* Set a new position and read more data */ pos = 140; if( fsetpos( stream, &pos ) != 0 ) printf( fsetpos error ); fread( buffer, sizeof( char ), 10, stream ); printf( 10 bytes at byte %I64d: %.10sn, pos, buffer ); fclose( stream );
20、 輸出:10 bytes at byte 10: .C: This p10 bytes at byte 140: .C and the9.文件指針重新定位到開(kāi)始void rewind( FILE *stream )參數(shù):stream 文件指針?lè)祷刂担簾o(wú)注:執(zhí)行完本函數(shù)后,文件的指針將返回到開(kāi)始位置實(shí)例:#include void main( void ) FILE *stream; int data1, data2; data1 = 1; data2 = -37; if( (stream = fopen( rewind.out, w+ ) != NULL ) fprintf( stream,
21、%d %d, data1, data2 ); printf( The values written are: %d and %dn, data1, data2 ); rewind( stream ); fscanf( stream, %d %d, &data1, &data2 ); printf( The values read are: %d and %dn, data1, data2 ); fclose( stream ); 輸出:The values written are: 1 and -37The values read are: 1 and -3710取得文件指針?biāo)傅男衏har
22、*fgets( char *string, int n, FILE *stream );int fputs( const char *string, FILE *stream );參數(shù):fgets 的string 保存到的字符串緩沖 ,而fputs 的string 表示要寫入的字符串n表示從文件中讀出的字符串不超過(guò) n-1個(gè)字符,在讀入的最后一個(gè)字符后加上串結(jié)束標(biāo)志0stream 文件指針?lè)祷刂担撼晒Φ脑挿祷刈址麛?shù)組的首地址 否則返回NULL 源于實(shí)例:#include void main( void ) FILE *stream; char line100; if( (stream = fo
23、pen( fgets.c, r ) != NULL ) if( fgets( line, 100, stream ) = NULL) printf( fgets errorn ); else printf( %s, line); fclose( stream ); fputs( Hello world from fputs.n, stdout );輸出:This program uses fgets to displayHello world from fputs.11關(guān)閉文件函數(shù)int _fcloseall( void ) int fclose( FILE *stream )參數(shù):stream 文件指針 返
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- JJF 2182-2024農(nóng)灌機(jī)井取水量計(jì)量監(jiān)測(cè)方法
- JJF 2165-2024實(shí)驗(yàn)室振動(dòng)式液體密度儀校準(zhǔn)規(guī)范
- 2024年度網(wǎng)絡(luò)游戲虛擬物品交易合同
- 2024年度建筑工程施工承包合同標(biāo)的明細(xì)
- 2024城市地下綜合管廊建設(shè)項(xiàng)目融資合同
- 2024年度放心簽建材銷售合同模板
- 2024年工程質(zhì)量檢測(cè)與環(huán)保評(píng)估合同
- 2024年度廣告發(fā)布合同標(biāo)的廣告內(nèi)容與投放時(shí)間
- 2024小產(chǎn)權(quán)房買賣合同糾紛
- 地理教學(xué)課件教學(xué)課件
- 2024年入團(tuán)知識(shí)考試題庫(kù)及答案
- 腫瘤化療導(dǎo)致的中性粒細(xì)胞減少診治中國(guó)專家共識(shí)(2023版)解讀
- 《新能源汽車概論》課件-6新能源汽車空調(diào)系統(tǒng)結(jié)構(gòu)及工作原理
- 2024年共青團(tuán)入團(tuán)考試題庫(kù)(附答案)
- 田徑運(yùn)動(dòng)會(huì)各種記錄表格
- 產(chǎn)科新生兒疫苗接種課件
- 企業(yè)信息管理概述課件
- 室外健身器材投標(biāo)方案(技術(shù)方案)
- 足浴店店長(zhǎng)聘用合同范本
- tubeless胸科手術(shù)麻醉
- 電商免責(zé)聲明范本
評(píng)論
0/150
提交評(píng)論