c - FILE 是常量或结构或两者

标签 c file

FILE

  1. 是常数吗? (想想,是大写的)(但是我读到,FILEstruct type in stdio.h )
  2. (or) 是一个结构?如果它是结构体,为什么它是大写的?

最佳答案

FILE 是一种不透明 类型。它通常是一个包含文件描述符、错误标志等的结构。但 C 标准对其内部结构只字不提。

为什么你需要知道?您将使用标准接口(interface),无需担心它是如何实现的。

FILE 的定义来自 C11 标准,§7.21.1(N1548 草案):

which is an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached;

并且没有提供任何进一步的信息。因此,实现可以自由地以他们认为合适的任何方式实现它。

举个例子,这是 glibc 实现所具有的。但是 - 正如我之前所说 - 它是一个实现细节,因此你不能依赖它:

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
};

struct _IO_FILE_complete
{
  struct _IO_FILE _file;
#endif
#if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
  _IO_off64_t _offset;
# if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
  /* Wide character stream stuff.  */
  struct _IO_codecvt *_codecvt;
  struct _IO_wide_data *_wide_data;
  struct _IO_FILE *_freeres_list;
  void *_freeres_buf;
# else
  void *__pad1;
  void *__pad2;
  void *__pad3;
  void *__pad4;
# endif
  size_t __pad5;
  int _mode;
  /* Make sure we don't get into trouble again.  */
  char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
#endif
};

关于c - FILE 是常量或结构或两者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40398958/

相关文章:

c - += 在 Visual C 中必须是左值

C 空指针算法

c - 为什么这个循环不连续打印相同的值?

c - 段错误 :core Dumped while using mvscanw function of ncurses in c on ubuntu

c++ - 静态编译 Python 解释器?

c - 如何确定 FIFO 中写入了多少条消息?

c - 从二进制文件读取并转换为 double ?

mysql - 将文件插入 SQL 字段并提取回文件

python - 将字符串按单词分隔到 Python 中的单独行

java - 防止文件竞争条件