Creating your own FILE * pointer in C?

I just looked at stdio.h where I could find the FILE structure definition:

typedef struct  {
   int             level;      /* fill/empty level of buffer */
   unsigned        flags;      /* File status flags          */
   char            fd;         /* File descriptor            */
   unsigned char   hold;       /* Ungetc char if no buffer   */
   int             bsize;      /* Buffer size                */
   unsigned char   *buffer;    /* Data transfer buffer       */
   unsigned char   *curp;      /* Current active pointer     */
   unsigned        istemp;     /* Temporary file indicator   */
   short           token;      /* Used for validity checking */
} FILE; 

Now I wonder if I can create myself (without using fopen) a valid FILE pointer to a thread, which I could use then in subsequent calls to fread or fwrite? this is a more theoretical question, so please don't be surprised why I want to know this;)

Also, does stdio not properly provide a routine to delete a file? In this case, I need OS calls, right?

thanks

+3
source share
5 answers

, , . C99 , FILE, .

stdio remove() .

+5

: FILE " ", . , ​​ .

, . FILE , " " .

POSIX (2) .

FILE , ( , , ).

, fopen() - fopen() .

remove().

+3

FILE - undefined. / / , .

FILE , .

.

OTOH , , , , ( ).

+1

BSD funopen fropen/fwopen FILE* /.

Linux fopencookie.

, , FILE fopen.

remove unlink .

+1

Depending on your standard library implementation, it may be possible to create your own structure FILEand use it. However, this is completely unsupported, may or may not work properly, and, of course, will not be portable between different library implementations.

To delete a file, call unlink()which (on my system) is in <unistd.h>.

0
source

Source: https://habr.com/ru/post/1716769/


All Articles