8

When I read pg.176 of The C programming Language by K&R, I was very excited. I found all the members of struct FILE ( which I was searching for ) and its just awesome to know how things work. But guess what, gcc complains, error: ‘FILE’ has no member named ‘fd’. It means things have changed now, I googled but could not find. Please Help, Thank you in advance.

I can use fileno() to get the file descriptor, but I hate working on abstraction level.

int
main ( int argc, char **argv ){

    FILE *fp = fopen ("ct.c", "r");
    printf ("%i", fp->fd);

    return 0;
}
dimSutar
  • 1,425
  • 2
  • 13
  • 17
  • Give us some code please to see where you are wrong – 3wic Jun 20 '13 at 08:46
  • 4
    If you really want to know how it works, read the source of libc (https://www.gnu.org/software/libc/). Knowing the members is not going to be all that helpful below a superficial level. Also, you probably can't see any of FILE*'s members because it's an opaque pointer. https://en.wikipedia.org/wiki/Opaque_pointer There's no reason for consuming code to know what's in a FILE*, so why bother giving them the possibility? Abstractions are good. If you were using FILE::fd and they decided to change it's name, your code would all break. If you're using fileno, nothing breaks. – Corbin Jun 20 '13 at 08:49
  • 3
    @Corbin thank you for those links, yeah abstraction is good when you writing code but not when you wanna understand some stuffs :D – dimSutar Jun 20 '13 at 08:53
  • That's the beauty of open-source: Just `grep` the source for it, and you know exactly what's going on. – cmaster - reinstate monica May 21 '18 at 19:30

3 Answers3

15

You need to look in your C library's source code.

Since you mention gcc and Linux, you're probably using the GNU libc, which is of course free software.

This file says:

/* The opaque type of streams.  This is the definition used elsewhere.  */
typedef struct _IO_FILE __FILE;

And this file declares the _IO_FILE structure:

struct _IO_FILE {
  int _flags;       /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

Chances are that the above, coming from a "real" production-quality library, is slightly more complicated than the example used in K&R. And, of course, you can't use this since it's library-internal and FILE is an opaque type, just as it says.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

The internal members change according to your compiler

As unwind said, you should look in your compiler's library code (in your case, GCC, which uses glibc).

The fileno "function" is actually a macro and is defined in MinGW (in stdio.h) like this :

    #define fileno(__F) ((__F)->_file)

Note that MinGW is based upon GCC, and shouldn't have compatibility problems.

Since it's a macro, you should be able to find it in your standard library by searching for "#define fileno" in your include folder. It is most likely defined in the stdio.h header.

Also, to answer your original question, here are the internal members of FILE as defined within stdio.h in GCC/MinGW :

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

tldr : in minGW/GCC, use fp->_file. Look at rest of post if you're using a different compiler

Gabriel Ravier
  • 358
  • 1
  • 6
  • 17
0

Answer of the @unwind is good, but I found another one workaround for the Unix-like systems with the GCC.

Unfortunately, the C has not support for the reflection (Reflection support in C), but you may can parse a output the C preprocessor.


Source code of shell script

setivolkylany$~/Downloads$ cat script.sh 

# a tempfile for source code on the C programming language
FILE_C=`tempfile`

# a tempfile for preprocessor`s output
FILE_I=`tempfile`

printf "#include \"stdio.h\"\nint main() {return 0;}" > $FILE_C
cpp $FILE_C > $FILE_I

# parse content of the tempfile for preprocessor`s output
# and display only the structure
print_it=false
while read line; do
    if [ "$line" == "struct _IO_FILE {" ]; then
        print_it=true
    fi;
    if [ "$print_it" = true ]; then
        echo $line
    fi;
    if [ "$line" == "};" ]; then
        print_it=false
    fi;
done < $FILE_I

# clean tempfiles
rm $FILE_C $FILE_I

Output

setivolkylany$~/Downloads$ ./script.sh 
struct _IO_FILE {
int _flags;




char* _IO_read_ptr;
char* _IO_read_end;
char* _IO_read_base;
char* _IO_write_base;
char* _IO_write_ptr;
char* _IO_write_end;
char* _IO_buf_base;
char* _IO_buf_end;

char *_IO_save_base;
char *_IO_backup_base;
char *_IO_save_end;

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _fileno;



int _flags2;

__off_t _old_offset;



unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];



_IO_lock_t *_lock;
# 293 "/usr/include/libio.h" 3 4
__off64_t _offset;
# 302 "/usr/include/libio.h" 3 4
void *__pad1;
void *__pad2;
void *__pad3;
void *__pad4;
size_t __pad5;

int _mode;

char _unused2[15 chrome-remote-desktop_current_amd64.deb data_structures_algorithms_tutorial.pdf dict-uk_ua-3-5-1.oxt getline.c jquery-3.1.1.min.js ld-linux.so (1).2 ld-linux.so.2 Makefile Portable Microsoft Office 2003.exe Python-3.5.2 script.sh teamviewer_12.0.71510_amd64.deb teamviewer_12.0.71510_i386.deb text_editor.zip sizeof (int) - 4 chrome-remote-desktop_current_amd64.deb data_structures_algorithms_tutorial.pdf dict-uk_ua-3-5-1.oxt getline.c jquery-3.1.1.min.js ld-linux.so (1).2 ld-linux.so.2 Makefile Portable Microsoft Office 2003.exe Python-3.5.2 script.sh teamviewer_12.0.71510_amd64.deb teamviewer_12.0.71510_i386.deb text_editor.zip sizeof (void *) - sizeof (size_t)];

};

This decision is no very delicate, but rather an attempt implementation reflection in the C programming language.

Community
  • 1
  • 1
PADYMKO
  • 4,217
  • 2
  • 36
  • 41