Reputation:
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
Upvotes: 3
Views: 3827
Reputation: 152807
On BSD, you can use funopen
or fropen
/fwopen
to create a FILE*
with custom callback functions for I/O.
On Linux, a similar function is fopencookie
.
Not that it's portable or recommended, but it's a way of creating valid FILE
pointers without using fopen
.
For file removal, there's remove
and unlink
pointed out by other answerers.
Upvotes: 1
Reputation: 70243
Even if you know your library inside out: One component of every FILE structure is the "file descriptor", usually an integer. This is how the kernel identifies the stream.
Your library cannot access the file system itself, it has to rely on system calls to the kernel. These system calls don't use the FILE structure to identify a stream, but merely the "file descriptor" from it.
On a POSIX system, you get that file descriptor from the open(2) call.
It is possible to get that file descriptor, and initialize the FILE structure yourself, if you know the library requirements well (and every library, even every library version, could vary widely in these requirements).
But then, you haven't done anything different than fopen() does for you - only fopen() is more comfortable.
For deleting a file, use remove().
Upvotes: 3
Reputation: 19233
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.
Upvotes: 5
Reputation: 108975
The content and meaning of FILE is undefined. Different compilers/libraries, and different versions of the same compiler/library can have completely different content, or even just subtle but breaking changes.
FILE is meant to be an opaque block of data that allows the different library functions to share state.
It is not designed to be broken apart and reused by user code.
OTOH there is nothing to stop you, other than the knowledge that any solution based on this will be fragile (at best).
Upvotes: 1
Reputation: 992837
Depending on your standard library implementation, it may be possible to create your own FILE
structure and use it. However, this is completely unsupported, may or may not actually work as expected, and certainly won't be portable between different library implementations.
To delete a file, call unlink()
, which (on my system) is in <unistd.h>
.
Upvotes: 0