VC读写函数详解(针对文件)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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: 100
2.文件的打开函数
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" );
else
printf( "The file 'data' was opened\n" );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "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 opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1
3.文件读取函数
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 );
}
else
printf( "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 );
}
else
printf( "File could not be opened\n" );
}
输出:
Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb
5.文件指针搜索函数
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 string
10
1.500000
7.按固定的格式读入数据函数
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-string
65000
3.141590
X
8.文件指针的定位和获取
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 p
10 bytes at byte 140: .C and the
9.文件指针重新定位到开始
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 -37
The values read are: 1 and -37
10.取得文件指针所指的行
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" );
else
printf( "%s", line);
fclose( stream );
}
fputs( "Hello world from fputs.\n", stdout );
}
输出:
This program uses fgets to display
Hello 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" );
else
printf( "The file 'data' was opened\n" );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "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 opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1
完。