why we use FILE * instead of FILE for I/O-Collection of common programming errors

Today I am learning things about Standard I/O of C. When I opened the stdio.h file found that:

typedef struct _iobuf FILE;

and when check the defination of struct _iobuf found that:

struct _iobuf {
    char *_ptr;
    int   _cnt;
    char *_base;
    int   _flag;
    int   _file;
    int   _charbuf;
    int   _bufsiz;
    char *_tmpfname;
};

To understand more, I have given descriptions about each don’t whether it is correct or not

struct _iobuf {
    char *_ptr;      /* next character position */
    int   _cnt;      /* characters left */
    char *_base;     /* location of buffer */
    int   _flag;     /* File status flags */
    int   _file;
    int   _charbuf;   /*Data transfer buffer */
    int   _bufsiz;    /* Buffer size */
    char *_tmpfname;  /* Temporary file indicator */
};

Now having two questions in my mind?

Q1: Have I provided the correct Names and how structure help in I/O and if I add or delete any thing what would happen? Does that would work accordingly? Does the sequence provided here matters?

Q2: There is no pointer used here but why use FILE * for opening the File?

  1. Have I provided the correct Names?

    These are all internal details that are specific to the Microsoft implementation, and AFAIK, undocumented.

    if I add or delete any thing what would happen?

    That would be really bad; you’d probably be causing undefined behaviour.

    There is no pointer used here but why use FILE * for opening the File?

    Because from the point-of-view of your application code, the implementation details don’t matter; FILE * is intended to be an opaque pointer.

  2. A1. Editing a standard header would result in undefined behaviour.

    A2. Structs are usually passed as pointers in C to avoid copying. Also, it’s meant to act as a handle or an opaque pointer.

    Bigger question is why would want to do anything you are asking about.

  3. Answers to all your questions:

    The Standard C Library by P.J. Plauger

Originally posted 2013-11-09 22:41:29.