FILE type in C language (Linux)
Asked Answered
S

2

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.

Selia answered 16/9, 2015 at 10:54 Comment(3)
It's an opaque type, you're not supposed to find it in a public place. It's also extremely implementation-specific.Amaryllidaceous
cplusplus.com/reference/cstdio/FILESwatow
Find some source code for fopen, fread and other f... functions (there must be tons of it out there) and adapt it to your needs.Watercourse
B
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

Beginning answered 16/9, 2015 at 11:23 Comment(0)
B
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 *
Bennington answered 16/9, 2015 at 11:34 Comment(4)
Because programm will start before the OS.Selia
So as kkk suggested you should look into /usr/include/libio.h if you are on linux.Bennington
Or just use open/read/write instead of badly reinventing the wheel.Numeration
You mean I woun't need to implement fopen, fwrite? Some simple functions? But it will be in a boot code...Selia

© 2022 - 2024 — McMap. All rights reserved.