admin 管理员组

文章数量: 887053


2024年1月6日发(作者:bracket是什么意思)

VC文件读写函数

1.文件的打开函数

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+"

打开文件并追加写入

源于

实例:

#include

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 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( stream ) )

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 opened

The file 'data2' was opened

Number of files closed by _fcloseall: 1

2.当前文件指针位置获取函数

long ftell( FILE *stream );

参数:stream 文件指针

返回值:当前文件指针的位置

实例:

#include

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: %ldn",

position );

fclose( stream );

}

}

输出:

Position after trying to read 100 bytes: 100

3.文件读取函数

size_t fread( void *buffer, size_t size, size_t count, FILE *stream )

参数: buffer 文件读取缓冲区 size 读取数据的类型 count 读取数据的个数

stream 文件指针

返回值:实际读取的数据个数

源于

例子:

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 文件指针

返回值:实际写入的数据个数

源于

实例:

#include

void main( void )

{

FILE *stream;

char list[30];

int i, numread, numwritten;

/* Open file in text mode: */

if( (stream = fopen( "", "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 itemsn", numwritten );

fclose( stream );

}

else

printf( "Problem opening the filen" );

if( (stream = fopen( "", "r+t" )) != NULL )

{

/* Attempt to read in 25 characters */

numread = fread( list, 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 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

文件的开始

源于

实例:

#include

void main( void )

{

FILE *stream;

char line[81];

int result;

stream = fopen( "", "w+" );

if( stream == NULL )

printf( "The file was not openedn" );

else

{

fprintf( stream, "The fseek begins here: "

"This is the file ''.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 ''.

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 的字节数

源于

实例:

#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( "", "w" );

fprintf( stream, "%s%c", s, c );

fprintf( stream, "%dn", i );

fprintf( stream, "%fn", fp );

fclose( stream );

system( "type " );

}

输出:

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(宏定义)

源于

实例:

#include

FILE *stream;

void main( void )

{

long l;

float fp;

char s[81];

char c;

stream = fopen( "", "w+" );

if( stream == NULL )

printf( "The file was not openedn" );

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( "%sn", s );

printf( "%ldn", l );

printf( "%fn", fp );

printf( "%cn", 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 否则 返回一个非零的数

源于

实例:

#include

void main( void )

{

FILE *stream;

fpos_t pos;

char buffer[20];

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 ), 10, 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 );

}

}

输出:

10 bytes at byte 10: .C: This p

10 bytes at byte 140: .C and the

9.文件指针重新定位到开始

void rewind( FILE *stream )

参数:stream 文件指针

返回值:无

注:执行完本函数后,文件的指针将返回到开始位置

实例:

#include

void main( void )

{

FILE *stream;

int data1, data2;

data1 = 1;

data2 = -37;

if( (stream = fopen( "", "w+" )) != NULL )

{

fprintf( stream, "%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 -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

源于

实例:

#include

void main( void )

{

FILE *stream;

char line[100];

if( (stream = fopen( "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 display

Hello world from fputs.

11.关闭文件函数

int _fcloseall( void )

int fclose( FILE *stream )

参数:stream 文件指针

返回值:fclose成功关闭返回 0 否则返回EOF 表示错误

_fcloseall 成功返回总共关闭的文件数 否则的话返回EOF 表示错误

实例:

#include

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 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( stream ) )

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 opened

The file 'data2' was opened

Number of files closed by _fcloseall: 1

VC文件读写函数


本文标签: 文件 指针 返回