Obtain filename from file pointer in C

Gaara picture Gaara · Mar 30, 2012 · Viewed 15.1k times · Source

Possible Duplicate:
Getting Filename from file descriptor in C
How get fileName having FILE*?

Is there any way where I can find the file_name from a file-pointer in C?

fp = fopen(file,"r");

From fp, is it possible to get the file name which I have opened?

Answer

Joshua Glazer picture Joshua Glazer · Mar 30, 2012

I believe not, because a file* could be to something that doesn't even have a name. There might be a platform dependent way, depending I. Your system, so if you don't care about portability, try looking at your compiler's definition of FILE if it has one. Odds are that your only way is to save the name when it's opened.

There are some other potentially hacky ways as well- check this link:

http://bytes.com/topic/c/answers/218921-how-get-filename-file-pointer

Cheers!