FILE结构旨在为不透明的.换句话说,如果您希望程序保持可移植性,则不应研究它.
此外,FILE总是通过指针使用,因此您甚至不需要知道其大小.
从某种意义上说,对于所有意图和目的,您都可以将其视为无效*.
现在,如果您真的对FILE类型的含义感兴趣,那么C标准本身就可以很好地解释它!参见C11 7.21.1p2:
(…) FILE which is an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached; (…)
如您所见,至少它包含类似以下内容:
>文件内的位置
>指向缓冲区的指针
>错误标志
> EOF标志
它提到(如您所做的)流.您可以在第7.21.2节“流”中找到有关它的更多详细信息:
Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported, for text streams and for binary streams.
(…)
A binary stream is an ordered sequence of characters that can transparently record internal data. (…)
如我们所见,流是字符的有序序列.注意,它没有说这个序列是否有限! (稍后会详细介绍)
那么,它们与文件有何关系?让我们看一下7.21.3文件:
A stream is associated with an external file (which may be a physical device) by opening a file, which may involve creating a new file. Creating an existing file causes its former contents to be discarded, if necessary. If a file can support positioning requests (such as a disk file, as opposed to a terminal), then a file position indicator associated with the stream is positioned at the start character number zero) of the file, unless the file is opened with append mode in which case it is implementation-defined whether the file position indicator is initially positioned at the beginning or the end of the file. The file position indicator is maintained by subsequent reads, writes, and positioning requests, to facilitate an orderly progression through the file.
(…)
看,当您打开“磁盘文件”(计算机中的典型文件)时,您正在关联一个“流”(在这种情况下为有限),您可以通过fread()打开/读取/写入/关闭/…. )及相关功能;包含所有必需信息的数据结构是FILE.
但是,还有其他类型的文件.想象一下伪随机数生成器.您可以将其概念化为一个无限的只读文件:每次阅读时,它都会赋予您不同的值,并且永远不会“结束”.因此,此文件将具有与其关联的无限流.并且某些操作可能对此没有意义(例如,您可能无法找到它,例如,移动文件位置指示器).
这只是作为快速介绍,但是您可以看到,FILE结构是对文件概念的抽象.如果您想了解更多有关此类内容的知识,则最好的办法是找到一本有关操作系统的好书,例如Modern Operating Systems from Tanenbaum.这本书还涉及C,因此更好.