为什么不FILE而不是文件*? [英] Why not FILE instead of FILE*?

查看:84
本文介绍了为什么不FILE而不是文件*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么文件的标准C类型处理FILE *而不是FILE?

AFAIK类型FILE是一种预定义的typedef,无论如何都适用于其他类型。

那么为什么不把它作为*指针*的typedef呢?对于

一件事,这会阻止人们试图在文件的内部徘徊

。另一方面,它可以允许真实的情况。类型
$ F $ b落后于FILE并不是指向任何东西的指针。


-

/ - Joona Palaste(pa *** **@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------- -----------规则! -------- /

有钱和小鸡肯定很酷。

- Beavis和Butt-head

Why is the standard C type for file handles FILE* instead of FILE?
AFAIK the type FILE is a pre-defined typedef for some other type anyway.
So why not make it instead a typedef for a *pointer* to that type? For
one thing, that would stop people trying to poke around at the insides
of a FILE. For another, it could allow for cases where the "real" type
behind FILE is not a pointer to anything.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head

推荐答案

Joona I Palasteaécrit:
Joona I Palaste a écrit :
为什么文件的标准C类型处理FILE *而不是文件?
AFAIK对于其他类型,类型FILE是一个预定义的typedef。
那么为什么不把它作为*指针*的typedef呢?对于
一件事,这会阻止人们试图在文件的内部徘徊。另一方面,它可以允许真实的情况。在FILE后面的类型不是指向任何东西的指针。
Why is the standard C type for file handles FILE* instead of FILE?
AFAIK the type FILE is a pre-defined typedef for some other type anyway.
So why not make it instead a typedef for a *pointer* to that type? For
one thing, that would stop people trying to poke around at the insides
of a FILE. For another, it could allow for cases where the "real" type
behind FILE is not a pointer to anything.




如果FILE不是指针肯定,则需要更改其他部分

的标准,就像fopen在失败时返回NULL。我担心这样的改变会破坏太多格式良好的代码。

此外,如果FILE不能创建你自己的typedef很容易

符合您的需求,因此不需要更改标准。


-

Richard



If FILE is not a pointer for sure, there is a need to change other parts
of the standard, like fopen returning NULL on failure. I am afraid that
such a change breaks too much well-formed code.
Moreover, it is very easy to create your own typedef if FILE does not
fit your need, so a change in the standard is not required.

--
Richard


2004年4月16日06:12:04 GMT,Joona I Palaste< pa ***** @ cc.helsinki.fi>

写道:
On 16 Apr 2004 06:12:04 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
为什么文件的标准C类型处理FILE *而不是FILE?
AFAIK类型FILE是一种预定义的typedef,无论如何都适用于其他类型。
那么为什么不把它作为*指针*的typedef呢?对于
一件事,这会阻止人们试图在文件的内部徘徊。另一方面,它可以允许真实的情况。在FILE后面的类型
不是指向任何东西的指针。
Why is the standard C type for file handles FILE* instead of FILE?
AFAIK the type FILE is a pre-defined typedef for some other type anyway.
So why not make it instead a typedef for a *pointer* to that type? For
one thing, that would stop people trying to poke around at the insides
of a FILE. For another, it could allow for cases where the "real" type
behind FILE is not a pointer to anything.




FILE *不是opaque类型的良好表示,因为

实现提供的宏可能依赖于FILE成员(即

getc)。


如果可以隐藏宏定义,则FILE不需要如此

耻骨。 (糟糕,公开。)


-

Sev



FILE * is not a good representation of an opaque type, since
implementation-provided macros may depend on FILE members (i.e.,
getc).

If macro definitions could be hidden, FILE would not need to be so
pubic. (Oops, public.)

--
Sev


Joona I Palaste< pa *****@cc.helsinki.fi>写道:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
为什么文件的标准C类型处理FILE *而不是FILE?
AFAIK类型FILE是一个预定义的typedef,无论如何都适用于其他类型。
所以为什么不把它作为*指针*的typedef呢?对于
一件事,这会阻止人们试图在文件的内部徘徊。另一方面,它可以允许真实的情况。在FILE后面的类型
不是指向任何东西的指针。
Why is the standard C type for file handles FILE* instead of FILE?
AFAIK the type FILE is a pre-defined typedef for some other type anyway.
So why not make it instead a typedef for a *pointer* to that type? For
one thing, that would stop people trying to poke around at the insides
of a FILE. For another, it could allow for cases where the "real" type
behind FILE is not a pointer to anything.




恕我直言制作文件指针到真实文件类型的typedef别名会有

在程序员中造成了更多的混乱,并且本身就不会让b






$ b $像以前一样在stdio.h中使用。

而不是 - >,这就是全部。


实际上,IMNSHO最合理的解决方案本来应该保持

的typedef,但隐藏底层类型的内部结构

将其声明从头部移动到库源(make

它是一个不透明的类型),例如类似于:

/ ************* stdio.h ************* /

typedef

struct _iobuf

FILE;


FILE * fopen(const char *,const char *);

/ * .... * /

/ ***************************** ****** /

/ ************* stdio.c ************* /

#include< stdio.h>


struct _iobuf

{

int _file;

int _flag;

/ *等* /

};


FILE * fopen(const char * filename,const char * mode)

{

/ * yaddayaddayadda * /;

}

/ *。 ... * /

/ *********************************** /

然而这种方法的缺点是getc和putc可能不是很容易实现的简单宏,但是给出了

的潜在危险从这样的宏观实施中产生,如果这将是一个巨大的损失(IMO没有),这是非常有争议的。


Rega rds

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq/

clc OT指南: http://benpfaff.org/writings/clc/off-topic.html



IMHO Making FILE a typedef alias for pointer-to-realfiletype would''ve
caused even more confusion among programmers, and on itself wouldn''t
have kept anybody from manipulating the internals of the underlying
type (lookup the struct declaration in stdio.h as before, and use .
instead of ->, that''s all).

Actually, the IMNSHO most reasonable solution would''ve been to keep
the typedef as is, but hide the internals of the underlying type by
moving its declaration from the header to the library source (make
it an opaque type), e.g. something like:
/************* stdio.h *************/
typedef
struct _iobuf
FILE;

FILE *fopen(const char *, const char *);
/* .... */
/***********************************/
/************* stdio.c *************/
#include <stdio.h>

struct _iobuf
{
int _file;
int _flag;
/* etc. */
};

FILE *fopen(const char *filename, const char *mode)
{
/* yaddayaddayadda */;
}
/* .... */
/***********************************/
This approach however has the drawback that getc and putc could
not have been easily implemented as simple macros, but given the
potential dangers that arise from such macro implementations,
it''s highly debatable if this would''ve been a big loss (IMO not).

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

这篇关于为什么不FILE而不是文件*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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