从文件中获取文件名* [英] Getting a File Name from a FILE *

查看:84
本文介绍了从文件中获取文件名*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己处于奇怪的情况,试图确定一个文件的名称

已经在其他地方通过fopen调用打开了,所以我只有
$ b来自该fopen电话的$ b FILE *。我仔细阅读了我最喜欢的在线C API参考

并没有找到方法,但希望这意味着我没有看到

正确的地方(我们都知道它有多容易一旦你知道

到实际看的东西就是找东西。


所以,给出一个文件*的问题可以回到这个名字/

文件的路径,如果是这样,怎么样?


感谢您的帮助,

Dale Pennington

I find myself in the odd situation of trying to determine the name of a file
that has been opened somewhere else with an fopen call, so all I have is the
FILE * from that fopen call. I perused my favorite online C API reference
and did not find a method, but hopefully that means I did not look in the
right places (we all know how easy it is to find stuff once you know where
to acutally look).

So, the question, given a FILE *, can one get back to the name/path of the
file that was opened, and if so, how ?

Thanks for the assist,
Dale Pennington

推荐答案

Dale Pennington写道:
Dale Pennington wrote:

所以,这个问题,给出一个文件* ,可以回到打开的

文件的名称/路径,如果是这样,怎么样?
So, the question, given a FILE *, can one get back to the name/path of the
file that was opened, and if so, how ?



No.


文件系统中可能有多个名称/路径引用

底层文件,或者根本没有,并且标准C中没有办法从一个FILE *转到一个零或多个文件名的列表,这些文件名对应于

it。


offtopic:根据你的平台和需求,如果你真的需要这样做,你可能会看一下像lsof这样的东西。不过,你真的是最好的消除这个需求。

No.

A FILE * may have many names/paths in the filesystem that refer to the
underlying file, or none at all, and there''s no way in standard C to go
from a FILE * to a list of zero or more filenames that correspond to
it.

offtopic: Depending on your platform and needs, you might look at
something like lsof if you really need to do this. You''re really best
off eliminating the need, though.





Dale Pennington在08/22/06 17:14写道:


Dale Pennington wrote On 08/22/06 17:14,:

我发现自己处于尝试确定文件名称的奇怪情况/>
已经通过fopen调用在其他地方打开了,所以我所拥有的就是来自该fopen调用的

FILE *。我仔细阅读了我最喜欢的在线C API参考

并没有找到方法,但希望这意味着我没有看到

正确的地方(我们都知道它有多容易一旦你知道

到实际看的东西就是找东西。


所以,给出一个文件*的问题可以回到这个名字/
打开的
文件的路径,如果是这样,怎么样?
I find myself in the odd situation of trying to determine the name of a file
that has been opened somewhere else with an fopen call, so all I have is the
FILE * from that fopen call. I perused my favorite online C API reference
and did not find a method, but hopefully that means I did not look in the
right places (we all know how easy it is to find stuff once you know where
to acutally look).

So, the question, given a FILE *, can one get back to the name/path of the
file that was opened, and if so, how ?



这是comp.lang.c中的问题19.15。经常

问题(FAQ)列表

http://www.c-faq.com/


....你不会喜欢这个答案。 : - (


-
Er ***** ****@sun.com

This is Question 19.15 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

.... and you''re not going to like the answer. :-(

--
Er*********@sun.com


" Dale Pennington" da *********** **** @ boeing.com写道:
"Dale Pennington" <da***************@boeing.comwrites:

所以,给定一个FILE *的问题,可以回到
的名称/路径
文件已打开,如果是,如何?
So, the question, given a FILE *, can one get back to the name/path of the
file that was opened, and if so, how ?



这是一个常见问题。


19.15:如何在给定开放流或文件的情况下恢复文件名

描述符?


答:这个问题通常是不可解决的。在Unix下,对于

实例,扫描整个磁盘(可能涉及特殊的

权限)理论上是必需的,如果

描述符是连接到一个管道或引用一个已删除的

文件(并且可能会给一个带有

多个链接的文件带来误导性答案。)最好记住文件的名称

和你一样打开它们(可能带有包装函数

围绕fopen())。


-

只是另一个C黑客。

This is a FAQ.

19.15: How can I recover the file name given an open stream or file
descriptor?

A: This problem is, in general, insoluble. Under Unix, for
instance, a scan of the entire disk (perhaps involving special
permissions) would theoretically be required, and would fail if
the descriptor were connected to a pipe or referred to a deleted
file (and could give a misleading answer for a file with
multiple links). It is best to remember the names of files
yourself as you open them (perhaps with a wrapper function
around fopen()).

--
Just another C hacker.


这篇关于从文件中获取文件名*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆