linux FILE 类型.

时间:2023-03-08 19:14:30

  stdio.h 头文件中,有以下内容(用内部行号解释):

 /* The opaque type of streams.  This is the definition used elsewhere.  */
typedef struct _IO_FILE FILE;
...
#include <libio.h>
...
/* Standard streams. */
extern struct _IO_FILE *stdin; /* Standard input stream. */
extern struct _IO_FILE *stdout; /* Standard output stream. */
extern struct _IO_FILE *stderr; /* Standard error output stream. */ #ifdef __STDC__
/* C89/C99 say they're macros. Make them happy. */
#define stdin stdin
#define stdout stdout
#define stderr stderr
#endif

  由上,知道 FILE 是 struct _IO_FILE 类型的别名,或者说 FILE 类型就是 struct _IO_FILE 类型,由 第143行中的 _IO_FILE,往上追溯至 include 预处理指令,我们可以在 libio.h 头文件中看到 _IO_FILE 结构体的定义:

 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[]; /* char* _save_gptr; char* _save_egptr; */ _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

  稍微注意的是(外侧行号解释), stdio.h 头文件中的第 4行优先于第 2 行执行,虽然第2行在第4行的前面.