如果打开文件,fopen() return 对文件指针有什么作用?
What does fopen() return to the file pointer if file is opened?
例如,如果我们声明一个文件指针 fp
并像这样打开一个文件:
FILE* fp = fopen("filename","w");
如果文件无法打开 fopen
returns NULL
到文件指针 fp
。如果文件打开,文件指针 fp
中存储了什么?
来自 fopen()
的手册页
Upon successful completion fopen()
return a
FILE
pointer. Otherwise, NULL
is returned and errno
is set to indicate
the error.
如果文件打开,文件指针中存储了什么? fopen()
returns FILE
结构,它只是别名结构 _IO_FILE
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE FILE; /* FILE is nothing but a structure which is _IO_FILE */
您可以在Linuxheadersstdio.h
中找到以上信息。关于 struct_IO_FILE
包含的信息可以在 Linux 机器的 libio.h
中找到,这是特定于平台的,即它因平台而异,如下所示(在终端中打开 /usr/include/libio.h
) 在 Linux 平台上。
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
};
C 委员会草案 N1570 关于 FILE*
的说明:
7.21.3 Files
...
- The address of the
FILE
object used to control a stream may be significant; a copy of a FILE
object need not serve in place of the original.
fopen()
返回的指针指向一个 FILE
结构,该结构的内容是特定于实现的(这意味着它们在不同的平台上是不同的)。
即使您知道特定实现中该结构的内容,您也不应尝试访问其任何成员或编写依赖于您对这些成员的了解的代码 (即使有可能做这样的事情)。
例如,如果我们声明一个文件指针 fp
并像这样打开一个文件:
FILE* fp = fopen("filename","w");
如果文件无法打开 fopen
returns NULL
到文件指针 fp
。如果文件打开,文件指针 fp
中存储了什么?
来自 fopen()
的手册页Upon successful completion
fopen()
return aFILE
pointer. Otherwise,NULL
is returned anderrno
is set to indicate the error.
如果文件打开,文件指针中存储了什么? fopen()
returns FILE
结构,它只是别名结构 _IO_FILE
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE FILE; /* FILE is nothing but a structure which is _IO_FILE */
您可以在Linuxheadersstdio.h
中找到以上信息。关于 struct_IO_FILE
包含的信息可以在 Linux 机器的 libio.h
中找到,这是特定于平台的,即它因平台而异,如下所示(在终端中打开 /usr/include/libio.h
) 在 Linux 平台上。
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
};
C 委员会草案 N1570 关于 FILE*
的说明:
7.21.3 Files
...
- The address of the
FILE
object used to control a stream may be significant; a copy of aFILE
object need not serve in place of the original.
fopen()
返回的指针指向一个 FILE
结构,该结构的内容是特定于实现的(这意味着它们在不同的平台上是不同的)。
即使您知道特定实现中该结构的内容,您也不应尝试访问其任何成员或编写依赖于您对这些成员的了解的代码 (即使有可能做这样的事情)。