我想知道结构文件的内部成员,最新游戏 [英] I Wanna know the Internal Members of struct FILE, the latest ones

查看:90
本文介绍了我想知道结构文件的内部成员,最新游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到K&放C编程语言的pg.176; R,我感到非常兴奋。我发现结构文件的所有成员(我正在寻找)和它只是真棒知道事情是如何工作的。但细想一下,GCC抱怨,错误:FILE没有名为成员的fd。这意味着,现在事情已经改变了,我用Google搜索,但没有找到。请帮帮忙,谢谢你在前进。

我可以使用的fileno()来获取的文件描述符,但我讨厌在抽象层面的工作。

  INT
主(INT ARGC,字符** argv的){    FILE *计划生育=的fopen(CT.C,R);
    的printf(%i的,FP-GT&; FD);    返回0;
}


解决方案

您需要在你的C库的源$ C ​​$ C的样子。

既然你提到的gcc和Linux,你可能使用GNU libc的,这当然是免费的软件。

<一个href=\"http://sourceware.org/git/?p=glibc.git;a=blob;f=libio/stdio.h;h=754301fc1abd789700a723fa399fecd3f931dfa2;hb=HEAD\">This文件说:

  / *不透明型流。这是在其他地方使用的定义。 * /
typedef结构_IO_FILE __FILE;

和<一个href=\"http://sourceware.org/git/?p=glibc.git;a=blob;f=libio/libio.h;h=3cf1712ea98d3c253f418feb1ef881c4a44649d5;hb=HEAD#l245\">this文件声明 _IO_FILE 结构:

 结构_IO_FILE {
  INT _flags; / *高位字是_IO_MAGIC;剩下的就是标志。 * /
#定义_IO_file_flags _flags  / *下面的指针对应于C ++流缓冲协议。 * /
  / *注:Tk的直接使用_IO_read_ptr和_IO_read_end字段。 * /
  字符* _IO_read_ptr; / *当前读指针* /
  字符* _IO_read_end; / *获取区域的结束。 * /
  字符* _IO_read_base; / *补篮开始+送面积。 * /
  字符* _IO_write_base; / *放区的开始。 * /
  字符* _IO_write_ptr; / *当前放指针。 * /
  字符* _IO_write_end; / *放区结束。 * /
  字符* _IO_buf_base; / *保留区启动。 * /
  字符* _IO_buf_end; / *预留区结束。 * /
  / *以下字段用于支持备份和还原。 * /
  字符* _IO_save_base; / *指针开始非流动GET区。 * /
  字符* _IO_backup_base; / *指向备份区的第一个有效字符* /
  字符* _IO_save_end; / *指针结束非流动GET区。 * /  结构_IO_marker * _markers;  结构_IO_FILE * _chain;  INT _fileno;
#如果0
  INT _blksize;
#其他
  INT _flags2;
#万一
  _IO_off_t _old_offset; / *这个曾经被_offset但它太小了。 * /#定义__HAVE_COLUMN / * *临时/
  PBASE的/ * 1 +柱号码(); 0是未知的。 * /
  无符号短_cur_column;
  符号字符_vtable_offset;
  烧焦_shortbuf [1];  / *字符* _save_gptr;字符* _save_egptr; * /  _IO_lock_t * _lock;
#IFDEF _IO_USE_OLD_IO_FILE
};

机会是,上述,从一个真正的产品质量图书馆来了,稍微比K&放大器使用的例子更加复杂; R。而且,当然,你不能的使用的这个,因为它是图书馆内部和文件是一个不透明的类型,就像它说。

When I read pg.176 of The C programming Language by K&R, I was very excited. I found all the members of struct FILE ( which I was searching for ) and its just awesome to know how things work. But guess what, gcc complains, error: ‘FILE’ has no member named ‘fd’. It means things have changed now, I googled but could not find. Please Help, Thank you in advance.

I can use fileno() to get the file descriptor, but I hate working on abstraction level.

int
main ( int argc, char **argv ){

    FILE *fp = fopen ("ct.c", "r");
    printf ("%i", fp->fd);

    return 0;
}

解决方案

You need to look in your C library's source code.

Since you mention gcc and Linux, you're probably using the GNU libc, which is of course free software.

This file says:

/* The opaque type of streams.  This is the definition used elsewhere.  */
typedef struct _IO_FILE __FILE;

And this file declares the _IO_FILE structure:

struct _IO_FILE {
  int _flags;       /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

Chances are that the above, coming from a "real" production-quality library, is slightly more complicated than the example used in K&R. And, of course, you can't use this since it's library-internal and FILE is an opaque type, just as it says.

这篇关于我想知道结构文件的内部成员,最新游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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