cengizhan
cengizhan

Reputation: 85

What do FILE struct members mean exactly in C?

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

I try to print them but I don't understand what they mean. Here What exactly is the FILE keyword in C? He said "Some believe that nobody in their right mind should make use of the internals of this structure." but he didn't explain what they mean.

Upvotes: 2

Views: 5120

Answers (2)

Pierre
Pierre

Reputation: 4406

_iobuf::_file can be used to get the internal file number, useful for functions that require the file no. Ex: _fstat().

Upvotes: 1

Andrew Henle
Andrew Henle

Reputation: 1

Look at the source code for your system's run-time libraries that implement the FILE-based IO calls if you want to know what those fields mean.

If you write code that depends on using those fields, it will be non-portable at best, utterly wrong at worst, and definitely easy to break. For example, on Solaris there are at least three different implementations of the FILE structure in just the normal libc runtime libraries, and one of those implementations (the 64-bit one) is opaque and you can't access any of the fields. Simply changing compiler flags changes which FILE structure your code uses.

And that's just one one version of a single OS.

Upvotes: 5

Related Questions