C 中的 FILE 关键字到底是什么?
我已经开始学习一些 C 作为一种爱好,并且已经盲目地使用 FILE 作为文件指针的声明相当长一段时间了,我一直在想。这是 C 用来处理文件的关键字或特殊数据类型吗?它是否包含文件内的流和其他数据?为什么定义为指针呢?
举一个例子来说明我的意思,让它更清楚一点:
FILE* fp; //<-- this
fp = fopen("datum.txt", "r");
while(!feof(fp)) {
// etc.
}
I've started learning some C as a hobby and have blindly used FILE as a declaration for file pointers for quite some time, and I've been wondering. Is this a keyword or special data type for C to handle files with? Does it contain a stream to the file within and other data? Why is it defined as a pointer?
An example to show what I mean to make it a little more clear:
FILE* fp; //<-- this
fp = fopen("datum.txt", "r");
while(!feof(fp)) {
// etc.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所指的是标准 io 库使用的 typedef 结构,用于保存 fopen 及其函数系列使用的适当数据。
使用指向结构的指针,您可以将其作为参数传递给函数。例如,这是 fgets 或 fgetc 所接受的,形式为 function(FILE* fp)
fopen 函数将返回一个指向新创建的 FILE 结构的指针,并将这个新指针分配给未使用的指针会让他们指向同一件事。
结构定义似乎比它的描述更虚幻一些。这直接取自我的 stdio.h,来自 MinGW32 5.1.4
其中包括之前的可爱评论:
这个结构的内容在其他实现上似乎有很大的变化,glibc 源代码通常有某种形式的注释,但它们的结构被隐藏在大量代码下。
留意上述警告是有意义的,而不必担心它会做什么。 :)
What you are refering to is a typedef'd structure used by the standard io library to hold the appropriate data for use of fopen, and its family of functions.
With a pointer to a struct, you can then pass it as a parameter to a function. This is for example what fgets or fgetc will accept, in the form of
function(FILE* fp)
The fopen function will return a pointer to a newly created FILE struct, assigning this new pointer to your unused one will cause them to point to the same thing.
The structure definition seems a little more illusive than its description. This is directly taken from my stdio.h, from MinGW32 5.1.4
Which includes the lovely comment before it:
The contents of this structure appear to change greatly on other implementations, the glibc sources usually have some form of commenting but their structure for this is burried under a lot of code.
It would make sense to heed the aforementioned warning and just not worry what it does. :)
FILE
是用作 typedef 名称的标识符,通常用于struct
。stdio 库通常有类似的东西
。所有处理 FILE 指针的 stdio 函数都知道
...
的内容,并且可以访问结构成员。 C 程序员必须使用诸如fopen
、feof
、ferror
、ungetc
等函数来创建和操作 FILE 结构。此类类型称为不透明(即您无法查看它们的内部,但必须使用访问器函数)。事实并非如此。这是一个结构体,您的代码声明了一个指针。请注意您的星号
,这是为什么星号应与变量标识符而不是类型名称一起使用的另一个示例:
FILE
is an identifier used as a typedef name, usually for astruct
.The stdio library usually has something like
somewhere. All stdio functions dealing with FILE pointers know the contens of
...
and can access the structure members. The C programmers must use functions likefopen
,feof
,ferror
,ungetc
etc to create and operate on FILE structures. Such types are called opaque (i.e. you can´t peek inside them but must use accessor functions).It isn't. It's a struct to which your code declares a pointer. Note the asterisk in your
which is another example of why the asterisk should go with the variable identifier, not the type name:
它不是关键字,而是 ANSI C 标准中定义的用于操作文件的数据类型。它通常指向向库函数描述文件及其当前状态的内部结构。
It's not a keyword, it's a data type defined in the ANSI C standard to operate with files. It usually points to an internal structure that describes the file and its current state to the library functions.
它是一种特殊的数据类型。它包含一个文件句柄以及各种 stdio 调用内部使用的各种标志。您永远不需要真正知道其中的内容,只需知道它是您可以传递的数据类型即可。
http://www.cplusplus.com/reference/clibrary/cstdio/FILE/
但是,如果您感兴趣,请参阅以下内容:
http://en.allexperts.com/q/C-1587/2008/5/FILE-Structure.htm
It's a special data type. It contains a file handle as well as various flags used internally by the various stdio calls. You'll never need to actually know what's in it, just that it's a data type that you can pass around.
http://www.cplusplus.com/reference/clibrary/cstdio/FILE/
However if you're interested, here's what it looks like:
http://en.allexperts.com/q/C-1587/2008/5/FILE-Structure.htm