Obtain filename from file pointer in C [duplicate]

删除回忆录丶 提交于 2019-12-18 08:33:59

问题


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?


回答1:


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!




回答2:


See here for why this is hard, and can't be done in a portable way.

http://discuss.fogcreek.com/joelonsoftware5/default.asp?cmd=show&ixPost=179112



来源:https://stackoverflow.com/questions/9937645/obtain-filename-from-file-pointer-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!