You can't. Not with just standard C.
On Linux you can do:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
int print_filename(FILE *f)
{
char buf[PATH_MAX];
char fnmbuf[sizeof "/prof/self/fd/0123456789"];
sprintf(fnmbuf,"/proc/self/fd/%d", fileno(f));
ssize_t nr;
if(0>(nr=readlink(fnmbuf, buf, sizeof(buf)))) return -1;
else buf[nr]='';
return puts(buf);
}
int main(void)
{
FILE * f = fopen ("/tmp/12345","wb");
if (0==f) return EXIT_FAILURE;
print_filename(f);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…