Is it possible to retrieve a file's name in C? If a file is created like the following:
FILE *f = fopen ("foo.txt", "r");
And the "foo.txt" comes from a variable out of scope. Is there a way to retrieve it from the FILE struct?
You can't in fact retrieve the file names from your FILE
objects.
However, if the name being passed to fopen
is important to you, and that variable is out of scope for whatever reason, you could always wrap a FILE*
in a struct, ie.
struct file_with_name {
FILE *f;
char *name; /* OR char name[MAX] */
}
At least that's what I'd do but it depends on what you're actually trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With