VC读写函数详解(针对文件)_第1页
VC读写函数详解(针对文件)_第2页
VC读写函数详解(针对文件)_第3页
VC读写函数详解(针对文件)_第4页
VC读写函数详解(针对文件)_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、VC读写函数详解1当前文件指针位置获取函数long ftell( FILE *stream );参数:stream 文件指针返回值:当前文件指针的位置实例:#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文件的打开函数FILE *fopen( const char *filename, const char *mode );FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );参数:filena

3、me 文件的名称mode 打开文件的模式返回值:成功的打开文件的话返回 文件的指针 否则返回NULL表示打开文件错误注:r 只读方式打开w 以写入方式打开a 从文件的尾端写入数据,要是文件不存在则创建后写入数据r+ 以读写方式打开(文件必须存在)w+ 打开一个可以读写的文件,如果该文件存在则覆盖写入a+ 打开文件并追加写入源于实例:#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.文件读取函数size_t fread( void *buffer, si

6、ze_t size, size_t count, FILE *stream )参数: buffer 文件读取缓冲区 size 读取数据的类型 count 读取数据的个数 stream 文件指针返回值:实际读取的数据个数源于例子:FILE* fp;char* buffer=new char1024;long realLength=fread(buffer,sizeof(char),1024,fp);/处理过程/delete buffer;fclose(fp);4.文件的写入函数size_t fwrite( const void *buffer, size_t size, size_t count

7、, FILE *stream );参数: buffer 所要写入文件的缓冲区 size 写入数据的类型 count 写入数据的个数 stream 文件指针返回值:实际写入的数据个数源于实例:#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.文件指针搜索函数int fseek( FILE *

10、stream, long offset, int origin )参数: stream 文件指针 offset 从当前指针开始的偏移量 origin 文件指针当前的位置返回值:成功返回0 否则返回非零值注:指针移动的时候是按字节移动的,要是成功的话 文件的指针将指向当前搜索到的位置origin 可以的取值在 stdio.h已经定义如下:SEEK_CUR 当前的文件指针SEEK_END 文件结束SEEK_SET 文件的开始源于实例:#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.按固定的格式写入数据函数int fprintf( FILE *stream, const char *format , argument .)int fwprintf( FILE *stream, const wchar_t *format , argument .)参数:stream 文件指针format 按照一定的格

13、式argument 可选参数列表返回值:fprintf 返回实际写入的字节数.fwprintf返回实际写入的wchar_t 的字节数源于实例:#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按固定的格式读入数据函数int fscanf( FILE *stream, const char *format , argument . )int fwscanf( FILE *stream, const wchar_t *format , argument . )参数:stream 文件指针format 按照一定的格式argument 可选参数列表返回值:fscanf 返回实际读入的字节数.fwscanf返回实际读入的wcha

15、r_t 的字节数如果返回值为 0 则说明没有被赋值如果有文件结束或是异常的IO错误时 返回 EOF(宏定义)源于实例:#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 );参数:stream 目标文件指针pos 文件指针的位置返回值:设置指针位置成功的话fsetpos返回0 否则 返回一个非零的数获得指针位置成功的话fgetpos返回0 否则 返回一个非零的数源于实例:#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.文件指针重新定位到开始void rewind( FILE *stream )参数:stream 文件指针返回值:无注:执行完本函数后,文件的指针将返回到开始位置实例:#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取得文件指针所指的行char

22、*fgets( char *string, int n, FILE *stream );int fputs( const char *string, FILE *stream );参数:fgets 的string 保存到的字符串缓冲 ,而fputs 的string 表示要写入的字符串n表示从文件中读出的字符串不超过 n-1个字符,在读入的最后一个字符后加上串结束标志0stream 文件指针返回值:成功的话返回字符数组的首地址 否则返回NULL 源于实例:#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关闭文件函数int _fcloseall( void ) int fclose( FILE *stream )参数:stream 文件指针 返

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论