Common misconception : Files have an EOF char at their end

青春壹個敷衍的年華 提交于 2019-12-03 20:33:50

PC => ^Z : EOF

in the olde PC-days the ctrl-Z was the signal in a file for EOF.

under UNIX and other modern systems: after reading behind the stat.st_size, EOF is signalled

sietschie

From http://www.drpaulcarter.com/cs/common-c-errors.php:

What is this value? EOF A common misconception of students is that files have a special EOF character at the end. There is no special character stored at the end of a file. EOF is an integer error code returned by a function.

Well, EOF is not stored at the end of the file, and is not char. EOF is an error message, that read functions return when there is not more data to read. This is the reason that getchar returns int - it may return a char converted to int, or the int EOF (which is not valid char, so if you got it, you can be sure that you passed the end of file).

EOF is not stored in the file. EOF (usually defined as -1) is returned by OS when there's no more data to read or an input error occurred. So once you reach the end of file, you must hit EOF.

My guess about the statement in your book is it meant "EOF is not necessarily be at the end of file but also possible to have it somewhere in the middle of a file". This is true on expected input error.

In the context of C code, EOF is simply an indication that there is no more data available for a given input stream. Whether that corresponds to a special EOF character in the file itself is a function of the underlying file or I/O system.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!