您的位置:首页 > 运维架构

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

2015-04-05 22:46 676 查看



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 byorigin.

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

streamPointer to a FILE object that identifies the stream.
offsetBinary files: Number of bytes to offset from origin.

Text files: Either zero, or a value returned by ftell.
originPosition 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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐