Search code examples
clinuxfiledescriptor

FILE type in C language (Linux)


Where can I find the declaration of type FILE in sources files?

I can't find it in stdio.h. I mean the declaration of it. Is it int type? File description? Or is it a structure?

I need to implement it in my code without standard library.


Solution

  • Not exactly a neat answer but will work

    Write some program like this say program.c

    #include <stdio.h>
    int main() {
            FILE *fp;
            return 0;
    }
    

    compile with debugging symbols

    gcc program.c -g -o program

    use gdb to see type

    # gdb ./program
    (gdb) b  main
    (gdb) run
    (gdb) ptype fp
    type = 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;
        short unsigned int _cur_column;
        signed char _vtable_offset;
        char _shortbuf[1];
        _IO_lock_t *_lock;
        __off64_t _offset;
        void *__pad1;
        void *__pad2;
        void *__pad3;
        void *__pad4;
        size_t __pad5;
        int _mode;
        char _unused2[20];
    } *
    

    or check

    /usr/include/stdio.h

    typedef struct _IO_FILE FILE;
    

    and

    /usr/include/libio.h

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

    In any case as @molbdnilo Pointed it is extremely implementation specific