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+"
打开文件并追加写入
源于<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
2.当前文件指针位置获取函数
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
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 否则返回非零值
write的返回值注:指针移动的时候是按字节移动的,要是成功的话 文件的指针将指向当前搜索到的位置
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>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。