EOF的本质

来源:互联网 发布:淘宝鹊桥量大吗 编辑:程序博客网 时间:2024/06/09 15:42
总结: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不过是文件类函数读到结尾时返回的一个结束标志
原创粉丝点击