Gordon L. Burditt Hi all:I have a FILE pointer that points to a file on disk (not any other iostream). 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 looklike itthankshilz 解决方案Not portably.--"When I have to rely on inadequacy, I prefer it to be my own."--Richard HeathfieldThis is highly system-dependent and likely very expensive. On somesystems, you can do a file-tree walk, stat() each file looking forone with a matching device and inode numbers. BUT: there''s noguarantee that a file still has a name. (unlinking an open fileis not guaranteed to destroy the file). There''s no guarantee thata file has only one name (even if you don''t follow symlinks). Andeven if it does have a name, there''s no guarantee that you havepermission to use it or even determine it. And, of course, there''sno guarantee that your system even HAS stat(), symlinks, devicenumbers, inode numbers, opendir(), readdir(), or unlink().File-tree walks on some systems (particularly large news serverswith terabytes of storage) can take days. This is not somethingyou want to do lightly.If you fopen()ed the file yourself, SAVE THE NAME if you''re goingto need it. If the FILE in question is stdin, stdout, or stderr,maybe the program interface should be changed so you supply afilename instead.Gordon L. Burditt 这篇关于如何获取FILE指针的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 13:36
查看更多