C语言 C中的FILE关键字到底是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5672746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:24:24  来源:igfitidea点击:

What exactly is the FILE keyword in C?

cfilestdio

提问by johnholtsdater

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?

我已经开始学习一些 C 作为一种爱好,并且盲目地使用 FILE 作为文件指针的声明已经有一段时间了,我一直在想。这是 C 用来处理文件的关键字或特殊数据类型吗?它是否包含到文件中的流和其他数据?为什么将其定义为指针?

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.
}

回答by Alexander

is this a keyword or special data type for C to handle files with?

这是 C 用来处理文件的关键字或特殊数据类型吗?

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.

您所指的是标准 io 库使用的 typedef 结构,用于保存用于 fopen 及其函数系列的适当数据。

Why is it defined as a pointer?

为什么将其定义为指针?

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)

使用指向结构的指针,您可以将其作为参数传递给函数。例如,这是 fgets 或 fgetc 将接受的,形式为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.

fopen 函数将返回一个指向新创建的 FILE 结构的指针,将此新指针分配给未使用的指针将导致它们指向相同的对象。

Does it contain a stream to the file within and other data?

它是否包含到文件中的流和其他数据?

The structure definition seems a little more illusive than its description. This is directly taken from my stdio.h, from MinGW32 5.1.4

结构定义似乎比它的描述更虚幻一些。这是直接取自我的 stdio.h,来自 MinGW32 5.1.4

typedef struct _iobuf
{
    char*   _ptr;
    int _cnt;
    char*   _base;
    int _flag;
    int _file;
    int _charbuf;
    int _bufsiz;
    char*   _tmpfname;
} FILE;

Which includes the lovely comment before it:

其中包括它之前的可爱评论:

Some believe that nobody in their right mind should make use of the internals of this structure.

有些人认为,头脑正常的人都不应该使用这种结构的内部结构。

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.

这个结构的内容在其他实现上似乎有很大变化,glibc 源通常有某种形式的注释,但它们的结构隐藏在大量代码下。

It would make sense to heed the aforementioned warning and just not worry what it does. :)

注意上述警告是有道理的,不要担心它会做什么。:)

回答by Jens

FILEis an identifier used as a typedef name, usually for a struct. The stdio library usually has something like

FILE是用作 typedef 名称的标识符,通常用于struct. stdio 库通常有类似的东西

typedef struct {
   ...
} FILE;

somewhere. All stdio functions dealing with FILE pointers know the contens of ...and can access the structure members. The C programmers must use functions like fopen, feof, ferror, ungetcetc 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).

某处。所有处理 FILE 指针的 stdio 函数都知道...结构成员的内容并且可以访问结构成员。在C程序员必须使用类似功能fopenfeofferrorungetc等上创建和文件结构进行操作。这种类型被称为opaque(即你不能窥视它们内部,但必须使用访问器函数)。

Why is it defined as a pointer?

为什么将其定义为指针?

It isn't. It's a struct to which yourcode declares a pointer. Note the asterisk in your

不是。它是一个结构,您的代码声明了指向它的指针。注意你的星号

FILE* fp;

which is another example of why the asterisk should go with the variable identifier, not the type name:

这是为什么星号应该与变量标识符一起使用的另一个例子,而不是类型名称:

FILE *fp;

回答by littleadv

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.

它不是关键字,而是 ANSI C 标准中定义的一种数据类型,用于对文件进行操作。它通常指向一个内部结构,该结构向库函数描述文件及其当前状态。

回答by Chris Eberle

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.

这是一种特殊的数据类型。它包含一个文件句柄以及各种 stdio 调用内部使用的各种标志。您永远不需要真正知道其中的内容,只需知道它是一种可以传递的数据类型。

http://www.cplusplus.com/reference/clibrary/cstdio/FILE/

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

http://en.allexperts.com/q/C-1587/2008/5/FILE-Structure.htm