Need of type "FILE"

Discussion in 'C Programming' started by BiGYaN, Dec 2, 2007.

  1. BiGYaN

    BiGYaN Guest

    I am not an expert in C, but from all the C code I've seen in my last
    4yrs of coding in C, I observe that programmers only use only "FILE *"
    type and never the actual "FILE" type. So my question is :

    <1> what is the actual use of this structure; i.e. can it be used
    anywhere in general programming? It is of course a very important
    structure, but can we make use of it a standard C program? I am told
    that using internals of this structure is not encouraged.

    <2> instead of having,
    typedef struct _iobuf
    {
    char* _ptr;
    int _cnt;
    char* _base;
    int _flag;
    int _file;
    int _charbuf;
    int _bufsiz;
    char* _tmpfname;
    } FILE;
    the stdio.h could have contained a type "pointer to FILE". This would
    save us writing the * every time.
    BiGYaN, Dec 2, 2007
    #1
    1. Advertising

  2. BiGYaN

    santosh Guest

    BiGYaN wrote:

    > I am not an expert in C, but from all the C code I've seen in my last
    > 4yrs of coding in C, I observe that programmers only use only "FILE *"
    > type and never the actual "FILE" type. So my question is :
    >
    > <1> what is the actual use of this structure; i.e. can it be used
    > anywhere in general programming? It is of course a very important
    > structure, but can we make use of it a standard C program? I am told
    > that using internals of this structure is not encouraged.


    That's correct. The details of the FILE type are implementation specific
    and any code that manipulates or otherwise relies on them will be tied
    to a particular implementation.

    Moreover the Standard library proved a full suite of functions to
    manipulate streams and there is nothing to be gained by messing around
    with implementation secrets.

    > <2> instead of having,
    > typedef struct _iobuf
    > {
    > char* _ptr;
    > int _cnt;
    > char* _base;
    > int _flag;
    > int _file;
    > int _charbuf;
    > int _bufsiz;
    > char* _tmpfname;
    > } FILE;
    > the stdio.h could have contained a type "pointer to FILE". This would
    > save us writing the * every time.


    No, I prefer some semblance of transparency in the source code.
    Excessive use of typedefs make the code very opaque. If an object is a
    pointer type then I want to know about it.
    santosh, Dec 2, 2007
    #2
    1. Advertising

  3. BiGYaN

    James Kuyper Guest

    BiGYaN wrote:
    > I am not an expert in C, but from all the C code I've seen in my last
    > 4yrs of coding in C, I observe that programmers only use only "FILE *"
    > type and never the actual "FILE" type. So my question is :
    >
    > <1> what is the actual use of this structure; i.e. can it be used
    > anywhere in general programming? It is of course a very important
    > structure, but can we make use of it a standard C program? I am told
    > that using internals of this structure is not encouraged.


    That is correct. The internals of this structure are the sole concern of
    the stdio library. You should never modify them. It is occasionally
    useful to examine the internals for debugging purposes, but only if
    you're pretty familiar with what they mean, which is highly
    implementation-specific.

    > <2> instead of having,
    > typedef struct _iobuf
    > {
    > char* _ptr;
    > int _cnt;
    > char* _base;
    > int _flag;
    > int _file;
    > int _charbuf;
    > int _bufsiz;
    > char* _tmpfname;
    > } FILE;
    > the stdio.h could have contained a type "pointer to FILE". This would
    > save us writing the * every time.


    That's certainly true, but experience has shown that typedefs for
    pointer types often lead to confusion. They are pointers, and behave
    like pointers, but don't obviously look like pointers.
    James Kuyper, Dec 2, 2007
    #3
  4. BiGYaN

    BiGYaN Guest

    Thank you both santosh and James Kuyper. Your answers enforced my
    beliefs further.

    But on the typedef for FILE * is something that I am not much
    convinced about .... I guess it is ultimately a matter of personal
    preference.
    BiGYaN, Dec 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. heyo
    Replies:
    3
    Views:
    881
    Dan Pop
    Apr 1, 2004
  2. pete
    Replies:
    4
    Views:
    779
    Dan Pop
    Apr 2, 2004
  3. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    878
    Yevgen Muntyan
    Feb 13, 2007
  4. kj
    Replies:
    5
    Views:
    396
  5. Roberto
    Replies:
    3
    Views:
    294
Loading...

Share This Page