文件操作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  exclusively to be used as arguments for this function:
Constant Reference position
SEEK_SET Beginning of file
SEEK_CUR Current position of the file pointer
SEEK_END End 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/


你可能感兴趣的:(C/C++)