究竟是什么在C文件的关键字? [英] What exactly is the FILE keyword in C?

查看:98
本文介绍了究竟是什么在C文件的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习一些C作为一种业余爱好,并盲目地使用文件作为在相当一段时间的指针文件声明,我一直在想。这是一个关键字或特殊的数据类型对于C来处理与文件?它包含的流中的文件和其它数据?为什么它定义为一个指针?

一个例子来说明我的意思,使其多一点明确的:

  FILE * FP; //<  - 这
FP = FOPEN(datum.txt,R);而(!的feof(...)//等。


解决方案

  

    

这是一个关键字或者对C与处理文件?特殊的数据类型。


  

你所指的是使​​用IO库的标准来保存相应的数据,使用一个的fopen结构typedef定义,其家庭的功能。


  

    

为什么定义为一个指针?


  

通过一个指向一个结构,然后你可以把它作为一个参数的函数。例如,这是什么FGETS或龟etc会接受,形式为函数(FILE * FP)

函数fopen将返回一个指向新创建的文件结构,分配这个新的指针到您使用的人会导致它们指向同一个东西。


  

    

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


  

的结构定义似乎比其描述多一点虚幻。这是直接从我的stdio.h中采取的mingw32从5.1.4

  typedef结构_iobuf
{
    字符* _ptr;
    INT _cnt;
    字符* _base;
    INT _flag;
    INT _file;
    INT _charbuf;
    INT _bufsiz;
    字符* _tmpfname;
}文件;

其中包括前可爱的评论:


  

    

有些人认为,没有人在他们的脑子应该利用的
     这种结构的内部结构。


  

此结构的内容出现在其他实现极大的改变,glibc的来源通常有某种形式的评论,但他们对这个结构是下了很大code的安葬。

这将是有意义听从上述警告,只是不用担心它做什么。 :)

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( ... ) //etc.

解决方案

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

这篇关于究竟是什么在C文件的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆