文件操作fopen, fclose, fread, fwrite, fseek, ftell

 

fopen

FILE * fopen ( const char * filename, const char * mode );


fclose

int fclose ( FILE * stream );


 

fread

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
Read block of data from stream
Reads an array of  count elements, each one with a size of  size bytes, from the  stream and stores them in the block of memory specified by  ptr.

The position indicator of the stream is advanced by the total amount of bytes read.

The total amount of bytes read if successful is  (size*count).

fwrite

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Write block of data to stream
Writes an array of  count elements, each one with a size of  size bytes, from the block of memory pointed by  ptr to the current position in the  stream.

The  position indicator of the stream is advanced by the total number of bytes written.

Internally, the function interprets the block pointed by  ptr as if it was an array of  (size*count) elements of type  unsigned char, and writes them sequentially to  stream as if  fputc was called for each byte.

fseek

int fseek ( FILE * stream, long int offset, int origin );
Reposition stream position indicator
Sets the position indicator associated with the  stream to a new position.

For streams open in binary mode, the new position is defined by adding  offset to a reference position specified by origin.

For streams open in text mode,  offset shall either be zero or a value returned by a previous call to  ftell, and  origin shall necessarily be  SEEK_SET.

If the function is called with other values for these arguments, support depends on the particular system and library implementation (non-portable).

The  end-of-file internal indicator of the  stream is cleared after a successful call to this function, and all effects from previous calls to  ungetc on this  stream are dropped.

On streams open for update (read+write), a call to  fseek allows to switch between reading and writing.

Parameters

stream
Pointer to a  FILE object that identifies the stream.
offset
Binary files: Number of bytes to offset from  origin.
Text files: Either zero, or a value returned by  ftell.
origin
Position used as reference for the  offset. It is specified by one of the following constants defined in  <cstdio>exclusively to be used as arguments for this function:
ConstantReference position
SEEK_SETBeginning of file
SEEK_CURCurrent position of the file pointer
SEEK_ENDEnd of file *
* Library implementations are allowed to not meaningfully support  SEEK_END (therefore, code using it has no real standard portability).

ftell

long int ftell ( FILE * stream );
Get current position in stream
Returns the current value of the position indicator of the  stream.

For binary streams, this is the number of bytes from the beginning of the file.

For text streams, the numerical value may not be meaningful but can still be used to restore the position to the same position later using  fseek (if there are characters put back using  ungetc still pending of being read, the behavior is undefined).


参考资料

http://www.cplusplus.com/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值