Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need of type "FILE"

Reply
Thread Tools

Need of type "FILE"

 
 
BiGYaN
Guest
Posts: n/a
 
      12-02-2007
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.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-02-2007
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.

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-02-2007
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.
 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      12-03-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type(d) != type(d.copy()) when type(d).issubclass(dict) kj Python 5 12-26-2010 06:48 PM
Need ideas -> generic storage of type registration with unknown type Noah Roberts C++ 1 03-15-2010 11:33 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57