开发者

What are the properties of the Objects of type FILE

FILE objects are usually created by a call to either fopen or tmpfile, which both return a reference to one of these objects.

Wha开发者_如何学编程t are the attributes of the Struct named FILE, or is it platform dependent?


The answer is it is platform dependent. At least using glibc on Linux, FILE is in fact a typedef from _IO_FILE which is implemented in libio.h:

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

As Martin York says in his comments, you are not meant to know about the internals of this structure. This particular definition of the structure is not guaranteed to be the same across implementations; I'd expect MSVC's file structure to be different.


FILE is a platform-dependent type, very probably a struct, that ultimately contains or points to at least:

  1. two offsets into the file opened, one for reading, one for writing;
  2. whether the file is opened for reading, for writing, or for both;
  3. whether the file is opened for binary or text I/O;
  4. EOF/error state of the stream;
  5. a buffer;
  6. a flag indicating block buffering or line buffering;
  7. char or wchar_t orientation;
  8. the last character put into the stream by ungetc.

If I'm not forgetting anything, this is all you can access with the stdio functions and those form the only interface for the use and inspection of FILE objects. Since FILE is opaque, you're not supposed to know how it keeps all this information.

POSIX in addition specifies that you can retrieve the underlying file descriptor with the fileno function/macro.


From draft n1256 of the C Language Standard, § 7.19.1, paragraph 2:

The types declared are size_t (described in 7.17);

    FILE

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

    fpos_t

which is an object type other than an array type capable of recording all the information needed to specify uniquely every position within a file.

The exact contents of the FILE type are implementation-dependant, and are not meant to be accessed directly.


Quick answer - sorry about this.

FILE is a handle, not a structure. It's basically represents a lock on the file you are either reading or writing to.

I would suggest reading the help relating to the fopen call for example, there are plenty of on-line help.

Hope this assists.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜