Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating own FILE* pointer in C?

I had just a look at the 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 am wondering if I could create myself (not using fopen) a valid FILE pointer to a stream which I could use then in subsequent calls for fread or fwrite? It is more a theoretical question so please do not wonder why I wanna know that ;)

Moreover, is it correct that stdio does not provide a routine to delete a file? In this case I need OS calls, don't I?

Thanks


1 Answers

No, you shouldn't even try to that as it's not portable. The C99 spec says that you even shouldn't try to copy an existing FILE object as it's address ‘may be significant’.

And stdio provides remove() function to remove a file.

like image 151
Michał Górny Avatar answered Feb 07 '26 13:02

Michał Górny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!