如何获取FILE指针的文件名 [英] how to get the filename of a FILE pointer

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

问题描述

大家好:

我有一个指向磁盘上文件的FILE指针(不是任何其他的io

流)。有没有办法获取该文件的文件名?

i认为它将是结构的_tmpfname部分,但它看起来并不像它那样b $ b b br />

谢谢

hilz

解决方案

" hilz" <无**** @ nowhere.com>写道:

我有一个指向磁盘上文件的FILE指针(不是任何其他的io
流)。有没有办法获取该文件的文件名?




不可移植。

-

" ;当我不得不依靠不足时,我更喜欢它是我自己的。

- 理查德希思菲尔德


>我有指向磁盘上文件的FILE指针(不是任何其他io

stream)。有没有办法获取该文件的文件名?
我认为它将是结构的_tmpfname部分,但它看起来不像它




这是高度依赖系统的,可能非常昂贵。在一些

系统上,你可以进行文件树遍历,stat()每个文件寻找

一个匹配的设备和inode号码。但是:没有

保证文件仍有名称。 (取消链接打开的文件

不保证销毁文件)。

文件只有一个名字(即使你不遵循符号链接)也不能保证。并且

即使它有一个名字,也不能保证你有

许可使用它甚至确定它。当然,还有

无法保证您的系统甚至可以使用stat(),符号链接,设备

数字,inode数字,opendir(),readdir( ),或取消链接()。


文件树在一些系统上行走(特别是大型新闻服务器

,带有太字节存储空间)可能需要数天。这不是你要轻易做的事情。


如果你自己fopen()编辑文件,如果你要去保存名字

需要它。如果有问题的文件是stdin,stdout或stderr,

可能会更改程序界面,所以你提供了一个

文件名。

Gordon L. Burditt


hilz写道:

我有一个指向文件的FILE指针磁盘
(不是任何其他I / O流)。
有没有办法获取* FILE的文件名?


No.

我以为它会是结构的_tmpfname部分,
但它看起来不像


Hi all:
I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn''t look
like it

thanks
hilz

解决方案

"hilz" <no****@nowhere.com> writes:

I have a FILE pointer that points to a file on disk (not any other io
stream). is there a way to get the filename of that FILE?



Not portably.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield


>I have a FILE pointer that points to a file on disk (not any other io

stream). is there a way to get the filename of that FILE?
i thought it would be the _tmpfname part of the struct, but it doesn''t look
like it



This is highly system-dependent and likely very expensive. On some
systems, you can do a file-tree walk, stat() each file looking for
one with a matching device and inode numbers. BUT: there''s no
guarantee that a file still has a name. (unlinking an open file
is not guaranteed to destroy the file). There''s no guarantee that
a file has only one name (even if you don''t follow symlinks). And
even if it does have a name, there''s no guarantee that you have
permission to use it or even determine it. And, of course, there''s
no guarantee that your system even HAS stat(), symlinks, device
numbers, inode numbers, opendir(), readdir(), or unlink().

File-tree walks on some systems (particularly large news servers
with terabytes of storage) can take days. This is not something
you want to do lightly.

If you fopen()ed the file yourself, SAVE THE NAME if you''re going
to need it. If the FILE in question is stdin, stdout, or stderr,
maybe the program interface should be changed so you supply a
filename instead.

Gordon L. Burditt


hilz wrote:

I have a FILE pointer that points to a file on disk
(not any other I/O stream).
is there a way to get the filename of that *FILE?
No.
I thought it would be the _tmpfname part of the struct,
but it doesn''t look like it



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

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