38

I've started learning some C as a hobby and have blindly used FILE as a declaration for file pointers for quite some time, and I've been wondering. Is this a keyword or special data type for C to handle files with? Does it contain a stream to the file within and other data? Why is it defined as a pointer?

An example to show what I mean to make it a little more clear:

FILE* fp; //<-- this
fp = fopen("datum.txt", "r");

while(!feof(fp)) {
   // etc.
}
ocirocir
  • 3,543
  • 2
  • 24
  • 34
johnholtsdater
  • 383
  • 1
  • 3
  • 4
  • 3
    Note that [`!feof(stream)` is always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – pmg May 10 '17 at 11:43

4 Answers4

60

is this a keyword or special data type for C to handle files with?

What you are refering to is a typedef'd structure used by the standard io library to hold the appropriate data for use of fopen, and its family of functions.

Why is it defined as a pointer?

With a pointer to a struct, you can then pass it as a parameter to a function. This is for example what fgets or fgetc will accept, in the form of function(FILE* fp)

The fopen function will return a pointer to a newly created FILE struct, assigning this new pointer to your unused one will cause them to point to the same thing.

Does it contain a stream to the file within and other data?

The structure definition seems a little more illusive than its description. This is directly taken from my stdio.h, from MinGW32 5.1.4

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

Which includes the lovely comment before it:

Some believe that nobody in their right mind should make use of the internals of this structure.

The contents of this structure appear to change greatly on other implementations, the glibc sources usually have some form of commenting but their structure for this is burried under a lot of code.

It would make sense to heed the aforementioned warning and just not worry what it does. :)

Jens
  • 69,818
  • 15
  • 125
  • 179
Alexander
  • 1,053
  • 11
  • 16
  • My first question and a great answer, Thank you for your input! This clears up some things indeed. – johnholtsdater Apr 15 '11 at 06:11
  • 5
    You don't need a pointer to pass `struct` around. The reason it's a pointer is to hide the structure. It's the C way of implementing `private` class members. This is exactly so that noone would try to use the structure itself, because the standard doesn't define the structure, it only requires a pointer to it. As mentioned before - it's called an **opaque** type. – littleadv Apr 15 '11 at 06:16
  • @littleadv I wonder which one of the [two](http://stackoverflow.com/a/5672769/5994041) statements is actually true about the `FILE` being available in standard. Anyway, what would be the reason behind mentioning `FILE` pointer, yet not defining what it actually is/how does it behave? Seems weird, like using a function from a header yet without the header being available anywhere? – Peter Badida May 13 '17 at 11:37
  • I've been look for this struct definition in my GCC directory for 20 minutes, and I've been going through my university's server that we run C projects on. I've found stdio.h on both (interestingly enough neither were in the actual GCC directory), and neither one had a definition for this struct. Any clues on where they could be found on RedHat and Mint OSes? – Ungeheuer Nov 17 '17 at 02:14
  • 1
    @littleadv Pointer doesn't make the type opaque. In order to make it opaque it should be casted to void*. What you're talking about is the ability to use incomplete types which is indeed a capability that pointers allow. However, if the internal definition of FILE type is provided in the standard headers, then using pointers won't provide opacity. – SomeWittyUsername Apr 08 '18 at 07:22
  • [This answer](https://stackoverflow.com/questions/17209087/i-wanna-know-the-internal-members-of-struct-file-the-latest-ones#17209274) covers the glibc FILE struct . – bain May 04 '18 at 13:49
12

FILE is an identifier used as a typedef name, usually for a struct. The stdio library usually has something like

typedef struct {
   ...
} FILE;

somewhere. All stdio functions dealing with FILE pointers know the contens of ... and can access the structure members. The C programmers must use functions like fopen, feof, ferror, ungetc etc to create and operate on FILE structures. Such types are called opaque (i.e. you can´t peek inside them but must use accessor functions).

Why is it defined as a pointer?

It isn't. It's a struct to which your code declares a pointer. Note the asterisk in your

FILE* fp;

which is another example of why the asterisk should go with the variable identifier, not the type name:

FILE *fp;
Jens
  • 69,818
  • 15
  • 125
  • 179
  • 1
    This makes more sense, I had not known what opaque types were at all before. Thank you! – johnholtsdater Apr 15 '11 at 06:12
  • 1
    I don't see the argument for placing the asterisk with the variable identifier. – IS4 Apr 25 '18 at 09:37
  • 2
    @IllidanS4 Consider `FILE* fp1, fp2;` It *looks* as if fp1 and fp2 are the same type, but they are not. It's a deceiving declaration. – Jens Apr 25 '18 at 12:06
  • Yes, that's a valid argument, but it wasn't implied by the original example. – IS4 Apr 26 '18 at 14:08
3

It's not a keyword, it's a data type defined in the ANSI C standard to operate with files. It usually points to an internal structure that describes the file and its current state to the library functions.

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

It's a special data type. It contains a file handle as well as various flags used internally by the various stdio calls. You'll never need to actually know what's in it, just that it's a data type that you can pass around.

http://www.cplusplus.com/reference/clibrary/cstdio/FILE/

However if you're interested, here's what it looks like:

http://en.allexperts.com/q/C-1587/2008/5/FILE-Structure.htm

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • Thank you for these links, I wish I could plus one. – johnholtsdater Apr 15 '11 at 06:11
  • 3
    `After more than 19 years, and over two million questions answered, AllExperts.com is now closed. We apologize for any inconvenience.`. [Here](https://web.archive.org/web/20110412145449/http://en.allexperts.com/q/C-1587/2008/5/FILE-Structure.htm)'s a snapshot of how it probably looked when the answer was written. – GnP Oct 20 '18 at 00:15
  • Both links are dead. – Aaron Wright Jul 05 '23 at 15:26