檔案操作fopen, fclose, fread, fwrite, fseek, ftell

來源:互聯網
上載者:User
 
 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:
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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.