FILE structure - stdio.h

Hi All,

I am new to linux and Programming.
Inside the file stdio.h, there is a description about FILE structure. Which has many internal data members like _p, _r, _flags etc.

I have written a sample code to find out the contents of the FILE structure.
It opens a sample file ( FILE *fp ), does some read/write operations on it. and at the end prints
fp->_p, fp->_r etc. contents.

But everytime it prints 0, or NULL for these values. !!!

Any idea.?

Thanks,
Nikunj

The contents of the FILE struct are implementaton defined. Meaning they can change any time the C maintainers or OS developers need to do that.

This is Solaris 9's idea of a FILE struct. I would dig thru you /usr/include file tree and find what your version looks like. Plus, consider chacking the internal file pointer and other struct members after you have done just a single read operation - like fgets. If you wait until EOF, you get NULL pointers.

struct __FILE_TAG       /* needs to be binary-compatible with old versions */
{
#ifdef _STDIO_REVERSE
        unsigned char   *_ptr;  /* next character from/to here in buffer */
        ssize_t         _cnt;   /* number of available characters in buffer */
#else
        ssize_t         _cnt;   /* number of available characters in buffer */
        unsigned char   *_ptr;  /* next character from/to here in buffer */
#endif
        unsigned char   *_base; /* the buffer */
        unsigned char   _flag;  /* the state of the stream */
        unsigned char   _file;  /* UNIX System file descriptor */
        unsigned        __orientation:2; /* the orientation of the stream */
        unsigned        __ionolock:1;   /* turn off implicit locking */
        unsigned        __seekable:1;   /* is file seekable? */
        unsigned        __filler:4;
};

Thnx for replying.!
I understand that the contents of FILE structure are implementation defined. For a unix system, x86 processor. I wanted to know, How can we find contents of FILE structure ,( Which is defined in stdio.h ) for a particular file and has data members like _p, _r etc.

Suppose I create a simple file with FILE *fp. and perform some read/write operations on it.
Now when I print (fp->_p) , (fp->_r) etc. like members. I always get NULL/0 value. While the program compiles successfully. !!

Kindly answer. !

Well, without seeing your code, how can we possibly advise you as to what is wrong except in general terms.

Here's my sample code.

#include<stdio.h>
#include<string.h>

int main()
{
int *i, j;
size_t n;
const char *buf;
unsigned char *t;
FILE *fp;

fp = fopen("test.txt", "r+" );

buf = "This is Test String , This is Test String\n" ;

//j = strlen(buf);
//j = strlen(fp->_p);

n = fwrite(buf, 1, 10, fp);

if( fp->_p )
        printf(" Its not null");

//i = (int *)memcpy((void *)t, (const void *)buf, (size_t)(10)) ;

j = (int)fp->_r ;
t = fp->_p ;
printf("j = %d , t = %x \n", j, t);

return 0;
}

This will give error with gcc:

fwrite_test.c: In function �main':
fwrite_test.c:22: error: �FILE' has no member named �_p'
fwrite_test.c:27: error: �FILE' has no member named �_r'
fwrite_test.c:28: error: �FILE' has no member named �_p'

But as I said, the FILE structure defined in stdio.h has all these members. !! How do these members get values.?

If they're all not defined, what's this NULL value thing you were talking about?

I suspect the struct you're seeing is not really FILE but _FILE or something like that. FILE itself will be an opaque type, because you're really not supposed to use anything inside. You'll need to typecast the pointer into the proper type to get at it.

How to do that, I'm not so sure, because I can't see your stdio.h from here. In my system, the structure isn't defined in stdio.h at all, but libio.h, and even then, doesn't resemble yours at all! Different versions of libc I guess. As they say, implementation-defined.