1

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.

John Smith
  • 835
  • 1
  • 7
  • 19
Maxim Gusev
  • 213
  • 3
  • 10

2 Answers2

2

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

asio_guy
  • 3,667
  • 2
  • 19
  • 35
0

Why do you want to implement it without standard library? Please use standard library if possible.

I think this will give you an idea:

#ifndef _FILE_DEFINED
#define _FILE_DEFINED
typedef struct _iobuf
{
    char*   _ptr;
    int _cnt;
    char*   _base;
    int _flag;
    int _file;
    int _charbuf;
    int _bufsiz;
    char*   _tmpfname;
} FILE;
#endif  /* Not _FILE_DEFINED *
Gaurav Pathak
  • 1,065
  • 11
  • 28