EOF的本質

總結:EOF只是函數的一個返回值,值爲十進制-1,十六進制爲0xFF,本質上爲函數的一個返回值,文件中並不存在EOF

我們先一起來看看FILE是怎麼定義的:
  FILE                          <STDIO.H>

File control structure for streams.

  typedef struct {
    short          level;
    unsigned       flags;    char           fd;
    unsigned char  hold;
    short          bsize;
    unsigned char *buffer, *curp;
    unsigned       istemp;
    short          token;
  } FILE;

再來看看這個flags是怎麼定義的:
  _F_xxxx                           <STDIO.H>

File status flags of streams

  Name    ?Meaning
  _F_RDWR ?Read and write
  _F_READ ?Read-only file
  _F_WRIT ?Write-only file
  _F_BUF  ?Malloc'ed buffer data
  _F_LBUF ?Line-buffered file
  _F_ERR  ?Error indicator
  _F_EOF  ?EOF indicator
  _F_BIN  ?Binary file indicator
  _F_IN   ?Data is incoming
  _F_OUT  ?Data is outgoing
  _F_TERM ?File is a terminal
}

在來看看EOF在頭文件中是怎麼定義的:
/*EOF a constant indicating that the end-of-file has been reached on a file*/

#define _F_EOF  0x0020                  /* EOF indicator        */
#define EOF     (-1)                    /* End of file indicator */
EOF在與fread等文件函數的返回值做比較時,時替換爲(-1)的
在文件中根本不存在EOF這個東西,EOF不過是文件類函數讀到結尾時返回的一個結束標誌
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章