VC读写函数详解(针对文件)

合集下载

VC读写函数详解(针对文件)

VC读写函数详解(针对文件)

VC读写函数详解1.当前文件指针位置获取函数long ftell( FILE *stream );参数:stream 文件指针返回值:当前文件指针的位置实例:#include <stdio.h>FILE *stream;void main( void ){long position;char list[100];if( (stream = fopen( "ftell.c", "rb" )) != NULL ){/* Move the pointer by reading data: */fread( list, sizeof( char ), 100, stream );/* Get position after read: */position = ftell( stream );printf( "Position after trying to read 100 bytes: %ld\n",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 );参数:filename 文件的名称mode 打开文件的模式返回值:成功的打开文件的话返回文件的指针否则返回NULL表示打开文件错误注:"r"只读方式打开"w"以写入方式打开"a"从文件的尾端写入数据,要是文件不存在则创建后写入数据"r+"以读写方式打开(文件必须存在)"w+"打开一个可以读写的文件,如果该文件存在则覆盖写入"a+"打开文件并追加写入源于<stdio.h>实例:#include <stdio.h>FILE *stream, *stream2;void main( void ){int numclosed;/* Open for read (will fail if file "data" does not exist) */if( (stream = fopen( "data", "r" )) == NULL )printf( "The file 'data' was not opened\n" );elseprintf( "The file 'data' was opened\n" );/* Open for write */if( (stream2 = fopen( "data2", "w+" )) == NULL )printf( "The file 'data2' was not opened\n" );elseprintf( "The file 'data2' was opened\n" );/* Close stream */if( fclose( stream ) )printf( "The file 'data' was not closed\n" );/* All other files are closed: */numclosed = _fcloseall( );printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }输出:The file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 13.文件读取函数size_t fread( void *buffer, size_t size, size_t count, FILE *stream ) 参数: buffer 文件读取缓冲区 size 读取数据的类型 count 读取数据的个数stream 文件指针返回值:实际读取的数据个数源于<stdio.h>例子:FILE* fp;char* buffer=new char[1024];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, FILE*stream );参数: buffer 所要写入文件的缓冲区 size 写入数据的类型 count 写入数据的个数 stream 文件指针返回值:实际写入的数据个数源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char list[30];int i, numread, numwritten;/* Open file in text mode: */if( (stream = fopen( "fread.out", "w+t" )) != NULL ){for ( i = 0; i < 25; i++ )list[i] = (char)('z' - i);/* Write 25 characters to stream */numwritten = fwrite( list, sizeof( char ), 25, stream );printf( "Wrote %d items\n", numwritten );fclose( stream );}elseprintf( "Problem opening the file\n" );if( (stream = fopen( "fread.out", "r+t" )) != NULL ){/* Attempt to read in 25 characters */numread = fread( list, sizeof( char ), 25, stream );printf( "Number of items read = %d\n", numread );printf( "Contents of buffer = %.25s\n", list );fclose( stream );}elseprintf( "File could not be opened\n" );}输出:Wrote 25 itemsNumber of items read = 25Contents of buffer = zyxwvutsrqponmlkjihgfedcb5.文件指针搜索函数int fseek( FILE *stream, long offset, int origin )参数: stream 文件指针 offset 从当前指针开始的偏移量 origin 文件指针当前的位置返回值:成功返回0 否则返回非零值注:指针移动的时候是按字节移动的,要是成功的话文件的指针将指向当前搜索到的位置origin 可以的取值在 stdio.h已经定义如下:SEEK_CUR当前的文件指针SEEK_END文件结束SEEK_SET文件的开始源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char line[81];int result;stream = fopen( "fseek.out", "w+" );if( stream == NULL )printf( "The file fseek.out was not opened\n" );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" ); fgets( 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 按照一定的格式argument 可选参数列表返回值:fprintf 返回实际写入的字节数.fwprintf返回实际写入的wchar_t 的字节数源于<stdio.h>实例:#include <stdio.h>#include <process.h>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, "%d\n", i );fprintf( stream, "%f\n", 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返回实际读入的wchar_t 的字节数如果返回值为 0 则说明没有被赋值如果有文件结束或是异常的IO错误时返回 EOF(宏定义)源于<stdio.h>实例:#include <stdio.h>FILE *stream;void main( void ){long l;float fp;char s[81];char c;stream = fopen( "fscanf.out", "w+" );if( stream == NULL )printf( "The file fscanf.out was not opened\n" );else{fprintf( stream, "%s %ld %f%c", "a-string",65000, 3.14159, 'x' );/* 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( "%s\n", s );printf( "%ld\n", l );printf( "%f\n", fp );printf( "%c\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 否则返回一个非零的数源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;fpos_t pos;char buffer[20];if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )printf( "Trouble opening file\n" );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 ), 10, stream );printf( "10 bytes at byte %I64d: %.10s\n", 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: %.10s\n", pos, buffer );fclose( stream );}}输出:10 bytes at byte 10: .C: This p10 bytes at byte 140: .C and the9.文件指针重新定位到开始void rewind( FILE *stream )参数:stream 文件指针返回值:无注:执行完本函数后,文件的指针将返回到开始位置实例:#include <stdio.h>void main( void ){FILE *stream;int data1, data2;data1 = 1;data2 = -37;if( (stream = fopen( "rewind.out", "w+" )) != NULL ){fprintf( stream, "%d %d", data1, data2 );printf( "The values written are: %d and %d\n", data1, data2 ); rewind( stream );fscanf( stream, "%d %d", &data1, &data2 );printf( "The values read are: %d and %d\n", data1, data2 );fclose( stream );}}输出:The values written are: 1 and -37The values read are: 1 and -3710.取得文件指针所指的行char *fgets( char *string, int n, FILE *stream );int fputs( const char *string, FILE *stream );参数:fgets 的string 保存到的字符串缓冲,而fputs 的string 表示要写入的字符串n表示从文件中读出的字符串不超过 n-1个字符,在读入的最后一个字符后加上串结束标志'\0'stream 文件指针返回值:成功的话返回字符数组的首地址否则返回NULL源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char line[100];if( (stream = fopen( "fgets.c", "r" )) != NULL ){if( fgets( line, 100, stream ) == NULL)printf( "fgets error\n" );elseprintf( "%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 文件指针返回值:fclose成功关闭返回 0 否则返回EOF 表示错误_fcloseall 成功返回总共关闭的文件数否则的话返回EOF 表示错误实例:#include <stdio.h>FILE *stream, *stream2;void main( void ){int numclosed;/* Open for read (will fail if file "data" does not exist) */if( (stream = fopen( "data", "r" )) == NULL )printf( "The file 'data' was not opened\n" );elseprintf( "The file 'data' was opened\n" );/* Open for write */if( (stream2 = fopen( "data2", "w+" )) == NULL )printf( "The file 'data2' was not opened\n" );elseprintf( "The file 'data2' was opened\n" );/* Close stream */if( fclose( stream ) )printf( "The file 'data' was not closed\n" );/* All other files are closed: */numclosed = _fcloseall( );printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }输出:The file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 1完。

visual studio 文件操作函数

visual studio 文件操作函数

visual studio 文件操作函数Visual Studio是一款功能强大的集成开发环境(Integrated Development Environment,简称IDE),广泛用于开发各种类型的应用程序。

在Visual Studio中,文件操作是开发过程中非常常见和重要的一部分。

本文将介绍一些常用的Visual Studio文件操作函数,帮助开发者更加高效地处理文件相关操作。

一、文件创建与打开1. `CreateFile`函数:用于创建或打开一个文件。

该函数可以指定文件名、访问权限、共享模式等参数,并返回一个表示文件句柄的值。

2. `fopen`函数:用于打开一个文件,并返回一个指向该文件的指针。

通过该指针,可以进行文件读写操作。

二、文件读写操作1. `ReadFile`函数:用于从文件中读取数据。

该函数可以指定文件句柄、读取的字节数、读取数据的缓冲区等参数,并返回读取的字节数和读取结果。

2. `WriteFile`函数:用于向文件中写入数据。

该函数可以指定文件句柄、要写入的数据、写入的字节数等参数,并返回写入的字节数和写入结果。

3. `fgets`函数:用于从文件中读取一行数据。

该函数可以指定要读取的字节数、读取数据的缓冲区和文件指针,并返回指向读取数据的指针。

三、文件定位与指针操作1. `fseek`函数:用于定位文件指针的位置。

该函数可以指定文件指针、偏移量和起始位置,并返回定位结果。

2. `ftell`函数:用于获取文件指针的当前位置。

该函数可以指定文件指针,并返回当前位置的偏移量。

3. `rewind`函数:用于将文件指针重新定位到文件的起始位置。

该函数可以指定文件指针,并无返回值。

四、文件关闭与删除1. `fclose`函数:用于关闭一个已打开的文件。

该函数可以指定要关闭的文件指针,并返回关闭结果。

2. `remove`函数:用于删除一个文件。

该函数可以指定要删除的文件名,并返回删除结果。

CC++关于文件的读写操作以及文件的打开和保存

CC++关于文件的读写操作以及文件的打开和保存

CC++关于⽂件的读写操作以及⽂件的打开和保存通过近来的学习,总结⼀下关于⽂件的读写操作以及⽂件的打开和保存的⽅法。

⼀、⽂件的读写操作:(1)C语⾔对⽂件的读写操作在C语⾔中,对⽂件的读写操作是⽤FILE结构体和常⽤的对⽂件操作的函数实现的,下⾯总结⼀下C语⾔中对⽂件操作的常⽤函数:fopen() 打开以⽂件名指定的⽂件fwrite() 写⽂件fread() 读⽂件fseek() 移动⽂件的指针到新的位置通过该函数的参数设定的偏移量和初始位置rewind() 移动⽂件的指针到⽂件流的开始位置,在通常情况下可⽤fseek()实现相同的功能,但⼆者有区别ftell() 获得当前⽂件指针的位置,常⽤该函数获得⽂件的⽂件流的长度fflush() 刷新缓冲区中的内容,当⽂件写⼊时直接将⽂件流的内容从缓冲区写⼊磁盘,当读取⽂件时直接将磁盘⽂件写⼊缓冲区,⽽不必等程序运⾏结束或关闭程序。

fclose() 关闭⽂件memset() 在⼀段内存块中填充某个给定的值⽰例代码如下:/*********************************************************** C语⾔实现⽂件写操作 ************************************************************/FILE *pFile=fopen("CLanguage.txt","w");fwrite("CLanguage",1,strlen("CLanguage"),pFile);//fseek(pFile,0,SEEK_SET);//fwrite("实现⽂件写操作",1,strlen("实现⽂件写操作"),pFile);fclose(pFile);/*********************************************************** C语⾔实现⽂件读操作 ************************************************************/FILE *pFile=fopen("CLanguage.txt","r");//char ch[100];//memset(ch,0,100);//fread(ch,1,100,pFile);//MessageBox(ch);char* pBuf;int length;fseek(pFile,0,SEEK_END);length=ftell(pFile);pBuf=new char[length+1];//fseek(pFile,0,SEEK_SET);rewind(pFile);fread(pBuf,1,length,pFile);pBuf[length]=0;MessageBox(pBuf);fclose(pFile);注意:在⽤C语⾔实现⽂件的读操作时,要注意⽂件的指针位置。

c文件读写函数

c文件读写函数

c文件读写函数C文件读写函数是在C语言中用于处理文件的函数。

它们允许我们从文件中读取数据或向文件中写入数据。

在本文中,我们将介绍一些常用的C文件读写函数,并讨论它们的用法和注意事项。

1. fopen函数:用于打开一个文件,并返回一个指向该文件的指针。

它接受两个参数,第一个参数是要打开的文件的路径和名称,第二个参数是打开文件的模式(如只读、写入等)。

打开文件成功后,该函数将返回一个指向文件的指针,否则返回NULL。

2. fclose函数:用于关闭一个打开的文件。

它接受一个参数,即要关闭的文件的指针。

关闭文件后,该指针将不再有效。

3. fgetc函数:用于从文件中读取一个字符,并将该字符作为整数返回。

它接受一个参数,即要读取的文件的指针。

4. fputc函数:用于将一个字符写入文件。

它接受两个参数,第一个参数是要写入的字符,第二个参数是要写入的文件的指针。

5. fgets函数:用于从文件中读取一行字符,并将它们保存在一个字符串中。

它接受三个参数,第一个参数是保存读取字符的字符串的指针,第二个参数是要读取的最大字符数,第三个参数是要读取的文件的指针。

6. fputs函数:用于将一个字符串写入文件。

它接受两个参数,第一个参数是要写入的字符串,第二个参数是要写入的文件的指针。

7. fread函数:用于从文件中读取一定数量的数据,并将它们保存在一个缓冲区中。

它接受四个参数,第一个参数是保存读取数据的缓冲区的指针,第二个参数是每个数据项的大小(以字节为单位),第三个参数是要读取的数据项的数量,第四个参数是要读取的文件的指针。

8. fwrite函数:用于将一定数量的数据从一个缓冲区写入文件。

它接受四个参数,第一个参数是保存要写入数据的缓冲区的指针,第二个参数是每个数据项的大小(以字节为单位),第三个参数是要写入的数据项的数量,第四个参数是要写入的文件的指针。

9. fseek函数:用于在文件中定位到指定的位置。

它接受三个参数,第一个参数是要定位的文件的指针,第二个参数是要移动的字节数,第三个参数是起始位置(如文件开头、当前位置、文件末尾)。

VC++中文件读写相关操作技巧

VC++中文件读写相关操作技巧

VC++中⽂件读写相关操作技巧CFile类的声明保存在afx.h头⽂件中。

CFile类是MFC⽂件类的基类,提供⾮缓冲⽅式的⼆进制磁盘输⼊、输出功能;并直接通过派⽣类来⽀持⽂本⽂件和内存⽂件。

提供访问本地⽂件内容的功能,不⽀持访问⽹络⽂件的功能。

CFile类的成员变量:m_hFile:表⽰⼀个打开⽂件的操作系统⽂件句柄。

通过对m_hFile 与 CFile::hFileNull的⽐较来判断该⽂件是否已经打开。

CFile类的成员函数:1、构造函数类CFile():在创建⼀个CFile对象时,我们可以采⽤3种⽅法实现。

A、CFile myFile;myFile.Open(LPCTSTR lpFileName,UINT nOpenFlags,CFileException * pError = NULL);B、CFile myFile(int hFile); 采⽤句柄⽅式创建使⽤该创建⽅法,在之前需要调⽤ CreateFile()函数,该函数的声明如下:HANDLE CreateFile(LPCTSTR lpFileName, ⽂件名称DWORD dwDesiredAccess, ⽂件访问的模式DWORD dwShareMode, ⽂件的共享模式LPSECURITY_ATTTRIBUTE lpSecurityAttribute,DWORD dwCreationDisposition, 怎么访问DWORD dwFlagsAndAttribute, ⽂件属性HANDLE hTemplateFile, 临时⽂件句柄);handle = ::CreateFile(“new.tmp”,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_FLAG_DELETE_ON_CLOSE,NULL);C、CFile(LPCTSTR lpFileName,UINT nOpenFlags); ⽂件名称,可以是相对路径,绝对路径或者⽹络路径。

c语言 文件操作函数

c语言 文件操作函数

c语言文件操作函数C语言文件操作函数C语言文件操作函数是C语言提供的一种文件操作方式,可以通过这些函数来读取、写入和修改文件。

文件操作函数可以帮助我们完成很多与文件有关的任务,比如读取配置文件、保存用户数据等。

在本文中,将介绍C语言中常用的文件操作函数。

打开文件在C语言中,我们可以使用fopen()函数来打开一个文件。

该函数的原型如下:FILE *fopen(const char *filename, const char *mode);其中,filename表示要打开的文件名,mode表示打开文件的模式。

mode的取值如下:- "r":以只读方式打开文件- "w":以写方式打开文件,如果文件不存在,则创建该文件;如果文件已存在,则清空该文件- "a":以写方式打开文件,如果文件不存在,则创建该文件;如果文件已存在,则在文件末尾追加数据- "r+":以读写方式打开文件,文件必须存在- "w+":以读写方式打开文件,如果文件不存在,则创建该文件;如果文件已存在,则清空该文件- "a+":以读写方式打开文件,如果文件不存在,则创建该文件;如果文件已存在,则在文件末尾追加数据例如,我们可以使用以下代码来打开一个名为example.txt的文件:FILE *fp;fp = fopen("example.txt", "w+");if (fp == NULL) {printf("Open file failed!\n");}在打开文件之后,我们可以使用fclose()函数来关闭文件,释放文件资源。

例如:fclose(fp);读取文件在C语言中,我们可以使用fscanf()函数从文件中读取数据。

该函数的原型如下:int fscanf(FILE *stream, const char *format, ...);其中,stream表示要读取数据的文件,format表示数据的格式。

VC++中对文件的写入和读取

VC++中对文件的写入和读取

VC++中对文件的写入和读取本文介绍两种方法对文件进行读取和写入操作:1、采用fstream 类;2、采用CStdioFile类。

CStdioFile继承自CFile,一个CStdioFile对象代表一个用运行时函数fopen 打开的C 运行时流式文件。

C++中的fstream类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O。

stream类有两个重要的运算符:插入器(<<)和析取器(>>)。

插入器(<<)即向流输出数据,析取器(>>)即从流中输入数据。

在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件fstream.h。

fstream类包括向“流”输出数据的ofstream类和从“流”中输出数据的ifstream类。

1、文件的写入往文件中写入数据的操作较为简单,这里以fstream类中往文件写入数据为例,介绍VC++中对文件的写入方法。

对于用CstdioFile 类对文件进行写入操作可参看相关资料。

下面给出采用fstream类对文件进行写入操作的代码:#include <fstream.h>ofstream of1;//创建对象of1.open("数据记录.txt",ios::out,filebuf::openprot);//打开文件int i;//定义一个整型变量float f; //定义一个单精度浮点型变量double d; //定义一个双精度浮点型变量i=123;//赋值f=3.478f; //赋值d=859.653; //赋值of1<<i<<’\t’<<f<<’\t’<<d;//写入数据of1.close();//关闭文件运行后打开“数据记录.txt”文件,内容见图1,变量i,f,d已写入文件中。

C文件读写函数介绍

C文件读写函数介绍

C++文件操作有多种方法,有操作系统提供API函数, C语言库函数,C++文件流类库, 开发工具提供的类库. 最重要的需要了解C语言库函数。

文件需要先打开, 然后才能进行读写, 最后需要关闭文件。

主要相关库函数介绍:1.打开文件FILE*fopen(const char*filename, const char*mode);fopen功能:打开一个文件,返回一个FILE指针;失败返回NULL。

参数filename指向要打开的文件名, 可以使用绝对路径,也可以使用相对路径;mode表示打开状态的字符串,其取值如下:字符串含义"r" 以只读方式打开文件"w" 以只写方式打开文件"a" 以追加方式打开文件"r+" 以读/写方式打开文件,如无文件出错"w+" 以读/写方式打开文件,如无文件生成新文件一个文件可以以文本模式或二进制模式打开,区别是:文本方式会按一定的方式对数据作相应的转换,而二进制模型不会对数据进行转换。

大多数应用使用二进制模式处理文件如图象,声音,WORD文件。

我们可以在模式字符串中指定打开的模式,加入"b"表示以二进制模式打开文件,加入"t"表示以文本模式打开文件, 默认为文本方式。

如"wb" 以只写方式打开二进制文件2. 关闭文件int fclose(FILE*stream);fclose()的功能就是关闭用fopen()打开的文件;如果成功,返回0,失败返回EOF。

文件系统有缓存, 在程序结束时一定要记得关闭打开的文件,不然可能会造成数据丢失的情况。

3. 读文件fread() 本函数一般用于二进制模式打开的文件中size_t fread(void *ptr, size_tsize, size_t n, FILE *stream);从流中读指定个数的字节,参数ptr是保存读取的数据,void*的指针可用任何类型的指针来替换,如char*、int *等等来替换;size是每块的字节数;n是读取的块数,如果成功,返回实际读取的块数(不是字节数;由于文件内容读完,实际可能小于n块)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VC读写函数详解1.当前文件指针位置获取函数long ftell( FILE *stream );参数:stream 文件指针返回值:当前文件指针的位置实例:#include <stdio.h>FILE *stream;void main( void ){long position;char list[100];if( (stream = fopen( "ftell.c", "rb" )) != NULL ){/* Move the pointer by reading data: */fread( list, sizeof( char ), 100, stream );/* Get position after read: */position = ftell( stream );printf( "Position after trying to read 100 bytes: %ld\n",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 );参数:filename 文件的名称mode 打开文件的模式返回值:成功的打开文件的话返回文件的指针否则返回NULL表示打开文件错误注:"r"只读方式打开"w"以写入方式打开"a"从文件的尾端写入数据,要是文件不存在则创建后写入数据"r+"以读写方式打开(文件必须存在)"w+"打开一个可以读写的文件,如果该文件存在则覆盖写入"a+"打开文件并追加写入源于<stdio.h>实例:#include <stdio.h>FILE *stream, *stream2;void main( void ){int numclosed;/* Open for read (will fail if file "data" does not exist) */if( (stream = fopen( "data", "r" )) == NULL )printf( "The file 'data' was not opened\n" );elseprintf( "The file 'data' was opened\n" );/* Open for write */if( (stream2 = fopen( "data2", "w+" )) == NULL )printf( "The file 'data2' was not opened\n" );elseprintf( "The file 'data2' was opened\n" );/* Close stream */if( fclose( stream ) )printf( "The file 'data' was not closed\n" );/* All other files are closed: */numclosed = _fcloseall( );printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }输出:The file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 13.文件读取函数size_t fread( void *buffer, size_t size, size_t count, FILE *stream ) 参数: buffer 文件读取缓冲区 size 读取数据的类型 count 读取数据的个数stream 文件指针返回值:实际读取的数据个数源于<stdio.h>例子:FILE* fp;char* buffer=new char[1024];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, FILE*stream );参数: buffer 所要写入文件的缓冲区 size 写入数据的类型 count 写入数据的个数 stream 文件指针返回值:实际写入的数据个数源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char list[30];int i, numread, numwritten;/* Open file in text mode: */if( (stream = fopen( "fread.out", "w+t" )) != NULL ){for ( i = 0; i < 25; i++ )list[i] = (char)('z' - i);/* Write 25 characters to stream */numwritten = fwrite( list, sizeof( char ), 25, stream );printf( "Wrote %d items\n", numwritten );fclose( stream );}elseprintf( "Problem opening the file\n" );if( (stream = fopen( "fread.out", "r+t" )) != NULL ){/* Attempt to read in 25 characters */numread = fread( list, sizeof( char ), 25, stream );printf( "Number of items read = %d\n", numread );printf( "Contents of buffer = %.25s\n", list );fclose( stream );}elseprintf( "File could not be opened\n" );}输出:Wrote 25 itemsNumber of items read = 25Contents of buffer = zyxwvutsrqponmlkjihgfedcb5.文件指针搜索函数int fseek( FILE *stream, long offset, int origin )参数: stream 文件指针 offset 从当前指针开始的偏移量 origin 文件指针当前的位置返回值:成功返回0 否则返回非零值注:指针移动的时候是按字节移动的,要是成功的话文件的指针将指向当前搜索到的位置origin 可以的取值在 stdio.h已经定义如下:SEEK_CUR当前的文件指针SEEK_END文件结束SEEK_SET文件的开始源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char line[81];int result;stream = fopen( "fseek.out", "w+" );if( stream == NULL )printf( "The file fseek.out was not opened\n" );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" ); fgets( 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 按照一定的格式argument 可选参数列表返回值:fprintf 返回实际写入的字节数.fwprintf返回实际写入的wchar_t 的字节数源于<stdio.h>实例:#include <stdio.h>#include <process.h>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, "%d\n", i );fprintf( stream, "%f\n", 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返回实际读入的wchar_t 的字节数如果返回值为 0 则说明没有被赋值如果有文件结束或是异常的IO错误时返回 EOF(宏定义)源于<stdio.h>实例:#include <stdio.h>FILE *stream;void main( void ){long l;float fp;char s[81];char c;stream = fopen( "fscanf.out", "w+" );if( stream == NULL )printf( "The file fscanf.out was not opened\n" );else{fprintf( stream, "%s %ld %f%c", "a-string",65000, 3.14159, 'x' );/* 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( "%s\n", s );printf( "%ld\n", l );printf( "%f\n", fp );printf( "%c\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 否则返回一个非零的数源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;fpos_t pos;char buffer[20];if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )printf( "Trouble opening file\n" );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 ), 10, stream );printf( "10 bytes at byte %I64d: %.10s\n", 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: %.10s\n", pos, buffer );fclose( stream );}}输出:10 bytes at byte 10: .C: This p10 bytes at byte 140: .C and the9.文件指针重新定位到开始void rewind( FILE *stream )参数:stream 文件指针返回值:无注:执行完本函数后,文件的指针将返回到开始位置实例:#include <stdio.h>void main( void ){FILE *stream;int data1, data2;data1 = 1;data2 = -37;if( (stream = fopen( "rewind.out", "w+" )) != NULL ){fprintf( stream, "%d %d", data1, data2 );printf( "The values written are: %d and %d\n", data1, data2 ); rewind( stream );fscanf( stream, "%d %d", &data1, &data2 );printf( "The values read are: %d and %d\n", data1, data2 );fclose( stream );}}输出:The values written are: 1 and -37The values read are: 1 and -3710.取得文件指针所指的行char *fgets( char *string, int n, FILE *stream );int fputs( const char *string, FILE *stream );参数:fgets 的string 保存到的字符串缓冲,而fputs 的string 表示要写入的字符串n表示从文件中读出的字符串不超过 n-1个字符,在读入的最后一个字符后加上串结束标志'\0'stream 文件指针返回值:成功的话返回字符数组的首地址否则返回NULL源于<stdio.h>实例:#include <stdio.h>void main( void ){FILE *stream;char line[100];if( (stream = fopen( "fgets.c", "r" )) != NULL ){if( fgets( line, 100, stream ) == NULL)printf( "fgets error\n" );elseprintf( "%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 文件指针返回值:fclose成功关闭返回 0 否则返回EOF 表示错误_fcloseall 成功返回总共关闭的文件数否则的话返回EOF 表示错误实例:#include <stdio.h>FILE *stream, *stream2;void main( void ){int numclosed;/* Open for read (will fail if file "data" does not exist) */if( (stream = fopen( "data", "r" )) == NULL )printf( "The file 'data' was not opened\n" );elseprintf( "The file 'data' was opened\n" );/* Open for write */if( (stream2 = fopen( "data2", "w+" )) == NULL )printf( "The file 'data2' was not opened\n" );elseprintf( "The file 'data2' was opened\n" );/* Close stream */if( fclose( stream ) )printf( "The file 'data' was not closed\n" );/* All other files are closed: */numclosed = _fcloseall( );printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }输出:The file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 1完。

相关文档
最新文档