What does fopen() return to the file pointer if file is opened?

2.8k Views Asked by At

For example if we declare a file pointer fp and open a file like this:
FILE* fp = fopen("filename","w");

If the file doesn't open fopen returns NULL to file pointer fp. What is stored in the file pointer fp if the file opens?

2

There are 2 best solutions below

1
On BEST ANSWER

The C Committee draft N1570 states this about FILE*:

7.21.3 Files
...

  1. 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.

The pointer returned by fopen() points to a FILE structure and the contents of that structure are implementation-specific (which means they are different in different platforms).

Even if you know the contents of that structure in a specific implementation, you should NOT try to access any of its members or write code that depends on your knowledge of those members (even if it were possible to do such a thing).

1
On

From the manual page of fopen()

Upon successful completion fopen() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.

what is stored in the file pointer if the file opens ? fopen() returns the FILE structure which is nothing but alias name of structure _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 */

you can find above information in Linux headers stdio.h. And information about what struct_IO_FILE contains can be find in libio.h in Linux machine which is platform specific i.e it varies according to platform, It looks like below(open /usr/include/libio.h in terminal) on Linux platform.

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