文件尾存在EOF吗?
参考:http://bbs.csdn.net/topics/290027166
我們先一起來看看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不過是文件類函數讀到結尾時返回的一個結束標志
另外,在谭浩强版C语言教程中也有说明:EOF是在stdio.h文件中定义的符号常量,值为-1。......如果在执行fgetc函数读字符时遇到文件结束符,函数返回一个文件结束标志EOF(即-1)。