Need of type "FILE"

B

BiGYaN

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

santosh

BiGYaN said:
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.
 
J

James Kuyper

BiGYaN said:
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.
 
B

BiGYaN

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top