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

发布于 2024-11-01 03:02:29 字数 258 浏览 1 评论 0原文

我已经开始学习一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

菩提树下叶撕阳。 2024-11-08 03:02:29

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

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

为什么定义为指针?

使用指向结构的指针,您可以将其作为参数传递给函数。例如,这是 fgets 或 fgetc 所接受的,形式为 function(FILE* fp)

fopen 函数将返回一个指向新创建的 FILE 结构的指针,并将这个新指针分配给未使用的指针会让他们指向同一件事。

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

结构定义似乎比它的描述更虚幻一些。这直接取自我的 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;

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

有些人认为,任何心智正常的人都不应使用
这个结构的内部。

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

留意上述警告是有意义的,而不必担心它会做什么。 :)

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

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.

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)

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.

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

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.

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

许你一世情深 2024-11-08 03:02:29

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

typedef struct {
   ...
} FILE;

。所有处理 FILE 指针的 stdio 函数都知道 ... 的内容,并且可以访问结构成员。 C 程序员必须使用诸如 fopenfeofferrorungetc 等函数来创建和操作 FILE 结构。此类类型称为不透明(即您无法查看它们的内部,但必须使用访问器函数)。

为什么定义为指针?

事实并非如此。这是一个结构体,您的代码声明了一个指针。请注意您的星号

FILE* fp;

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

FILE *fp;

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

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, 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).

Why is it defined as a pointer?

It isn't. It's a struct to which your code 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;
忆梦 2024-11-08 03:02:29

它不是关键字,而是 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.

书间行客 2024-11-08 03:02:29

它是一种特殊的数据类型。它包含一个文件句柄以及各种 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文